-2

我有一个名为 sample.xml 的自定义布局,如下所示,

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >

<TableLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" >

    <TextView
        android:id="@+id/textView3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dp"
        android:text="@string/Constituency" />

    <View
        android:layout_width="wrap_content"
        android:layout_height="2dip"
        android:layout_marginLeft="20dp"
        android:layout_marginRight="33dp"
        android:layout_marginTop="10dp"
        android:background="#000000" />

    <TableRow
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="0dp"
        android:layout_marginLeft="20dp"
        android:layout_marginTop="0dp" >

        <View
            android:layout_width="3dp"
            android:layout_height="40dp"
            android:background="#000000" />

        <TextView
            android:id="@+id/TextView04"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="10dp"
            android:layout_marginTop="0dp"
            android:text="@string/Nampally" />         
    </TableRow>
  </TableLayout>
</LinearLayout>

在 MainActivity 中,setContentView 设置为

setContentView(R.Layout.activity_main)

这是默认的 xml 文件。现在我想在我的应用程序启动时将内容视图设置为 sample.xml。

我给了

setContentView(R.Layout.sample);

当我运行我的应用程序时,它说不幸的是应用程序失败了!错误。

当我调试时,它显示一些空指针异常

这是我的日志猫,

04-04 03:55:40.691: D/AndroidRuntime(2174): Shutting down VM
04-04 03:55:40.691: W/dalvikvm(2174): threadid=1: thread exiting with uncaught exception (group=0x40a71930)
04-04 03:55:40.711: E/AndroidRuntime(2174): FATAL EXCEPTION: main
04-04 03:55:40.711: E/AndroidRuntime(2174): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.liveapp/com.example.liveapp.MainActivity}: java.lang.NullPointerException
04-04 03:55:40.711: E/AndroidRuntime(2174):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
04-04 03:55:40.711: E/AndroidRuntime(2174):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
04-04 03:55:40.711: E/AndroidRuntime(2174):     at android.app.ActivityThread.access$600(ActivityThread.java:141)
04-04 03:55:40.711: E/AndroidRuntime(2174):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
04-04 03:55:40.711: E/AndroidRuntime(2174):     at android.os.Handler.dispatchMessage(Handler.java:99)
04-04 03:55:40.711: E/AndroidRuntime(2174):     at android.os.Looper.loop(Looper.java:137)
04-04 03:55:40.711: E/AndroidRuntime(2174):     at android.app.ActivityThread.main(ActivityThread.java:5041)
04-04 03:55:40.711: E/AndroidRuntime(2174):     at java.lang.reflect.Method.invokeNative(Native Method)
04-04 03:55:40.711: E/AndroidRuntime(2174):     at java.lang.reflect.Method.invoke(Method.java:511)
04-04 03:55:40.711: E/AndroidRuntime(2174):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
04-04 03:55:40.711: E/AndroidRuntime(2174):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
04-04 03:55:40.711: E/AndroidRuntime(2174):     at dalvik.system.NativeStart.main(Native Method)
04-04 03:55:40.711: E/AndroidRuntime(2174): Caused by: java.lang.NullPointerException
04-04 03:55:40.711: E/AndroidRuntime(2174):     at com.example.liveapp.MainActivity.onCreate(MainActivity.java:49)
04-04 03:55:40.711: E/AndroidRuntime(2174):     at android.app.Activity.performCreate(Activity.java:5104)
04-04 03:55:40.711: E/AndroidRuntime(2174):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
04-04 03:55:40.711: E/AndroidRuntime(2174):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
04-04 03:55:40.711: E/AndroidRuntime(2174):     ... 11 more

这是我的 MainActivity.java,

  public class MainActivity extends Activity {
   public final android.content.Context Context = MainActivity.this;
     protected void onCreate(Bundle savedInstanceState) {
     super.onCreate(savedInstanceState);
      final LinearLayout samplelayout=(LinearLayout)findViewById(R.layout.sample);
     setContentView(R.layout.samplelayout);
  }
 }

我的代码有什么问题?

请帮忙!!

4

3 回答 3

1

如果您在从一个布局文件切换到另一个布局文件时收到 NullPointerException,则很可能您正在使用新布局中不存在的 findViewById 访问对 View 对象的引用。findViewById 返回一个空对象,所以当您调用一个方法或访问该对象的公共属性时,您将获得 NPE。

此外,sampleLayout 变量是多余的。

  final LinearLayout samplelayout=(LinearLayout)findViewById(R.layout.sample);
 setContentView(R.layout.samplelayout);

应该只是

 setContentView(R.layout.sample);
于 2013-04-04T03:58:17.413 回答
1

我不知道这是否是 NPE 的原因,但它肯定是错误的。在你的行

final LinearLayout samplelayout=(LinearLayout)findViewById(R.layout.sample);

您正在尝试获取对 LinearLayout 作为视图的引用,但您将文件的 id 传递给它。findViewById() 适用于视图 ID,而不是布局 ID。删除此行。

你只需要拥有。

setContentView(R.layout.samplelayout);

于 2013-04-04T04:10:59.280 回答
0

您正在尝试在LinearLayout创建之前获取对它的引用。您必须先设置内容视图,然后尝试按 id 查找视图。另一个问题是 id 错误。试试这个:


setContentView(R.layout.samplelayout);
final LinearLayout samplelayout=(LinearLayout)findViewById(R.layout.your_layout_id);
于 2014-03-20T10:11:08.323 回答