-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);

アプリケーションを実行すると、残念ながらアプリケーションが失敗しました!! と表示されます。エラー。

デバッグすると、nullpointer 例外が表示されます

これは私のlogcatです。

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

これが NPE の原因かどうかはわかりませんが、間違いなく間違っています。あなたのラインで

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

ビューとして LinearLayout への参照を取得しようとしていますが、ファイルの ID を渡しています。findViewById() は、レイアウト ID ではなくビュー ID で機能します。この行を削除します。

あなたは単に持っている必要があります。

setContentView(R.layout.samplelayout);

于 2013-04-04T04:10:59.280 に答える
1

あるレイアウト ファイルから別のレイアウト ファイルに切り替えるときに NullPointerException が発生した場合は、新しいレイアウトに存在しない findViewById を使用して View オブジェクトへの参照にアクセスしている可能性があります。findViewById は null オブジェクトを返すため、メソッドを呼び出すか、そのオブジェクトのパブリック プロパティにアクセスすると、NPE が取得されます。

また、sampleLayout 変数は冗長です。

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

単純にする必要があります

 setContentView(R.layout.sample);
于 2013-04-04T03:58:17.413 に答える