我的主要活动调用视图来膨胀我的main.xml但无论什么原因似乎总是返回null,我有点迷失在这里,因为这工作在4.2但现在没有将我的源码的其余部分升级到6.0
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
LayoutInflater inflater = (LayoutInflater) this.getSystemService(
Context.LAYOUT_INFLATER_SERVICE);
View layout = inflater.inflate(R.layout.main, null);
setContentView(layout);
ctx = this;
prefs = PreferenceManager.getDefaultSharedPreferences(this);
res = getResources();
mAudioManager = (AudioManager)getSystemService(Context.AUDIO_SERVICE);
mMediaButtonReceiver = new RemoteControlReceiver();
mediaFilter = new IntentFilter(Intent.ACTION_MEDIA_BUTTON);
mediaFilter.setPriority(Integer.MAX_VALUE);
registerReceiver(mMediaButtonReceiver, mediaFilter);
//mDrawable = (this.res.getDrawable(R.drawable.glowy_metal));
//mBackground.setBackgroundDrawable(mDrawable);
mBackground = (ImageView) layout.findViewById(R.id.background);
mBackground.setOnTouchListener(new backgroundTouchListener());
logcat在这里显示为null:
mBackground = (ImageView) layout.findViewById(R.id.background);
main.xml中的背景ID
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">
<ImageView
android:id="@+id/background"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:scaleType="fitCenter"
android:background="@drawable/glowy_metal"/>
任何人都可以对这里可能发生的事情有所了解吗?
logcat:
D/AndroidRuntime(12906): Shutting down VM
E/AndroidRuntime(12906): FATAL EXCEPTION: main
E/AndroidRuntime(12906): Process: com.cphelps76.CarHome, PID: 12906
E/AndroidRuntime(12906): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.cphelps76.CarHome/com.cphelps76.CarHome.CarHomeActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.view.View.setOnTouchListener(android.view.View$OnTouchListener)' on a null object reference
E/AndroidRuntime(12906): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2356)
E/AndroidRuntime(12906): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2418)
E/AndroidRuntime(12906): at android.app.ActivityThread.access$900(ActivityThread.java:154)
E/AndroidRuntime(12906): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1321)
E/AndroidRuntime(12906): at android.os.Handler.dispatchMessage(Handler.java:102)
E/AndroidRuntime(12906): at android.os.Looper.loop(Looper.java:135)
E/AndroidRuntime(12906): at android.app.ActivityThread.main(ActivityThread.java:5289)
E/AndroidRuntime(12906): at java.lang.reflect.Method.invoke(Native Method)
E/AndroidRuntime(12906): at java.lang.reflect.Method.invoke(Method.java:372)
E/AndroidRuntime(12906): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:904)
E/AndroidRuntime(12906): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:699)
E/AndroidRuntime(12906): Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.view.View.setOnTouchListener(android.view.View$OnTouchListener)' on a null object reference
E/AndroidRuntime(12906): at com.cphelps76.CarHome.CarHomeActivity.onCreate(CarHomeActivity.java:211)
E/AndroidRuntime(12906): at android.app.Activity.performCreate(Activity.java:5990)
E/AndroidRuntime(12906): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1106)
E/AndroidRuntime(12906): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2309)
E/AndroidRuntime(12906): ... 10 more
W/ActivityManager( 789): Force finishing activity 1 com.cphelps76.CarHome/.CarHomeActivity
答案 0 :(得分:3)
只需使用
setContentView(R.layout.main);
然后再
mBackground = (ImageView) findViewById(R.id.background);
无需创建新的inflater,因为Activity
已经有方法可以执行您想要的操作。