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

时间:2019-01-24 16:56:10

标签: android android-studio background runtime-error

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

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设置为Black时没有任何问题。请帮助我解决错误,并且仍然将背景色设置为我希望从资源中获得的背景色。感谢您。

1 个答案:

答案 0 :(得分:1)

要修复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);