尝试在 Resources res = getResources() 中调用虚拟方法;当我尝试设置背景颜色时



我正在尝试设置背景颜色。我想从我的资源中检索一种我想要的颜色,但后来出现错误。"尝试调用虚拟方法"。这是我的代码

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.location.Address;
import android.location.Geocoder;
import android.support.annotation.ColorInt;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.TextView;

import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.Marker;
import java.io.IOException;
import java.util.List;
public class InfoWindowAdapter extends Activity implements GoogleMap.InfoWindowAdapter {

    private Context context;
    public InfoWindowAdapter() {
    }
    public InfoWindowAdapter(Context context) {
        this.context = context.getApplicationContext();
    }

    public View getInfoWindow(Marker marker) {
        return null;

    }
    @Override
    public View getInfoContents(Marker marker) {
        LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View v = inflater.inflate(R.layout.activity_info_window_adapter, null);
        v.setBackgroundColor(getResources().getColor(R.color.trans_blue));
        //v.setBackgroundColor(Color.BLACK);

        LatLng latLng = marker.getPosition();
        Geocoder geocoder;
        List<Address> addresses;
        geocoder = new Geocoder(this);
        try {
            addresses = geocoder.getFromLocation(latLng.latitude, latLng.longitude, 1);
            String address = addresses.get(0).getAddressLine(0);
            TextView headline = v.findViewById(R.id.headline);
            TextView LatLng = (TextView) v.findViewById(R.id.address);
            TextView info = (TextView) v.findViewById(R.id.tv_info);
            headline.setText("Uw Locatie:");
            LatLng.setText(address);
            info.setText("Onthoud deze locatie voor het telefoongesprek");
        }
            catch (IOException e) {
            e.printStackTrace();
        }
        return v;
    }
}

在这里,我的日志猫


Attempt to invoke virtual method 'android.content.res.Resources android.content.Context.getResources()' on a null object reference
        at android.content.ContextWrapper.getResources(ContextWrapper.java:92)
        at android.view.ContextThemeWrapper.getResources(ContextThemeWrapper.java:81)
        at hk.ust.cse.comp107x.rsrrevelidatieservice.InfoWindowAdapter.getInfoContents(InfoWindowAdapter.java:53)
        at com.google.android.gms.maps.zzg.zzi(Unknown Source)
        at com.google.android.gms.maps.internal.zzi.dispatchTransaction(Unknown Source)
        at com.google.android.gms.internal.maps.zzb.onTransact(Unknown Source)
        at android.os.Binder.transact(Binder.java:387)
        at fs.a(:com.google.android.gms.dynamite_mapsdynamite@14799046@14.7.99 (040306-223214910):8)
        at com.google.android.gms.maps.internal.o.b(:com.google.android.gms.dynamite_mapsdynamite@14799046@14.7.99 (040306-223214910):16)
        at com.google.maps.api.android.lib6.impl.ce.a(:com.google.android.gms.dynamite_mapsdynamite@14799046@14.7.99 (040306-223214910):38)
        at com.google.maps.api.android.lib6.impl.ce.a(:com.google.android.gms.dynamite_mapsdynamite@14799046@14.7.99 (040306-223214910):60)
        at com.google.maps.api.android.lib6.gmm6.vector.v.a(:com.google.android.gms.dynamite_mapsdynamite@14799046@14.7.99 (040306-223214910):23)
        at com.google.maps.api.android.lib6.gmm6.api.f.c(:com.google.android.gms.dynamite_mapsdynamite@14799046@14.7.99 (040306-223214910):128)
        at com.google.maps.api.android.lib6.impl.df.a(:com.google.android.gms.dynamite_mapsdynamite@14799046@14.7.99 (040306-223214910):27)
        at com.google.maps.api.android.lib6.impl.dd.g(:com.google.android.gms.dynamite_mapsdynamite@14799046@14.7.99 (040306-223214910):179)
        at com.google.android.gms.maps.model.internal.q.a(:com.google.android.gms.dynamite_mapsdynamite@14799046@14.7.99 (040306-223214910):85)
        at ft.onTransact(:com.google.android.gms.dynamite_mapsdynamite@14799046@14.7.99 (040306-223214910):4)
        at android.os.Binder.transact(Binder.java:387)
        at com.google.android.gms.internal.maps.zza.transactAndReadExceptionReturnVoid(Unknown Source)
        at com.google.android.gms.internal.maps.zzv.showInfoWindow(Unknown Source)
        at com.google.android.gms.maps.model.Marker.showInfoWindow(Unknown Source)
        at hk.ust.cse.comp107x.rsrrevelidatieservice.MapsActivity$3.onComplete(MapsActivity.java:216)
        at com.google.android.gms.tasks.zzj.run(Unknown Source)
        at android.os.Handler.handleCallback(Handler.java:739)
        at android.os.Handler.dispatchMessage(Handler.java:95)
        at android.os.Looper.loop(Looper.java:148)
        at android.app.ActivityThread.main(ActivityThread.java:7325)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)

而当我使用setBackground到黑色时,没有任何问题。请帮助我如何修复错误,并仍然将背景颜色设置为我想要的资源颜色。提前谢谢。

要修复nullpointerException只需将

this.context = context

而不是

this.context = context.getApplicationContext();

v.setBackgroundColor(context.getResources().getColor(R.color.trans_blue));

而不是

v.setBackgroundColor(getResources().getColor(R.color.trans_blue));

此外,正如 @Mike M. 所说,您应该删除extends Activity,因为您的代码中不需要它并替换

geocoder = new Geocoder(this);

geocoder = new Geocoder(context);

最新更新