我在另一个java文件中有一个类,在我的MainActivity中调用。我需要膨胀外部类中的一些布局。我遇到的问题是指定上下文,因为当我尝试膨胀布局时,我得到一个空指针异常。该类没有它自己的onCreate()
方法,所以我需要从我的MainActivity传递上下文?不知道如何去做。这引起了我的NullPointerException
:LayoutInflater上的NullPointer - Android
Context context = getApplicationContext();
LayoutInflater inflater = (LayoutInflater)context.getApplicationContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);;
NullPointerException
上Context
方面:
public class CameraListener extends Activity implements OnCameraChangeListener {
private static final int SCALE_HEIGHT = 50;
GoogleMap mMap;
GoogleC3iActivity mParent;
Context context = getApplicationContext();
如果答案不起作用,请粘贴整个类(没有方法,只是代码的相关部分) – Siddharth
使用'LayoutInflater.from(context)'。 –
NPE在LayoutInflater – ono