2014-02-20 53 views
0

我想生一个设置我的内容查看这样的:无法设置内容查看,崩溃的应用程序

这是我的onCreate方法:

RelativeLayout mainView; 

protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    mainView = (RelativeLayout)findViewById(R.id.mainView); 
    setContentView(mainView); 
} 

的“MAINVIEW”是下面的XML文件:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:tools="http://schemas.android.com/tools" 
android:id="@+id/mainView" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:paddingBottom="@dimen/activity_vertical_margin" 
android:paddingLeft="@dimen/activity_horizontal_margin" 
android:paddingRight="@dimen/activity_horizontal_margin" 
android:paddingTop="@dimen/activity_vertical_margin" 
tools:context=".MainActivity" > 

</RelativeLayout> 

现在,启动应用程序时,我得到一些:

02-20 21:35:09.936: E/AndroidRuntime(15174): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.ochs.androidsnake/com.x.x.MainActivity}: java.lang.NullPointerException 

回答

2

mainviewnull,然后再设置内容。改用它。

protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.<name of your layout file>); 
    mainView = (RelativeLayout)findViewById(R.id.mainView); 
} 
+0

为我工作,谢谢! – user2410644

2

在致电setContentView()之前,您不能拨打findViewById()