【发布时间】:2013-06-30 00:49:13
【问题描述】:
我在 MainActivity 中调用的另一个 java 文件中有一个类。我需要在外部类中增加一些布局。我遇到的问题是指定上下文,因为当我尝试扩展布局时,我得到一个空指针异常。该类没有自己的 onCreate() 方法,所以我需要从 MainActivity 传递上下文?不知道该怎么做。这导致我NullPointerException:
Context context = getApplicationContext();
LayoutInflater inflater = (LayoutInflater)context.getApplicationContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);;
NullPointerException on Context 上下文:
public class CameraListener extends Activity implements OnCameraChangeListener {
private static final int SCALE_HEIGHT = 50;
GoogleMap mMap;
GoogleC3iActivity mParent;
Context context = getApplicationContext();
【问题讨论】:
-
如果答案不起作用,粘贴整个类(没有方法,只是代码的相关部分)
-
使用
LayoutInflater.from(context)。 -
NPE 在 LayoutInflater 之前
标签: android nullpointerexception android-context