2011-08-12 11 views
0

EDIT:解決策は私の質問の下のコメントにあります。App gettin NullPointerExceptionレイアウト内にビューがありません

私のアプリが強制終了している理由がわかりません。最近、私はxmlファイル( 'directory.xml'という名前)とjavaクラスファイル( 'Directory.java')を削除し、4つのxmlファイルとjavaファイル(directory1.xml、directory2.xml、directory3.xml、 directory4.xml、Directory1.java、Directory2.java、Directory3.java、Directory4.java)。これらの変更を含めるようにマニフェストを更新しました。 'mainselect.xml'というレイアウトのボタンがあります。 4つのボタンは、オンラインクリックリスナーがJavaファイル(Directory1-4.java)を呼び出すように設定されているボタンです。これはコンテンツレイアウトを関連する.xmlファイルに設定するだけです。それはすべてとても簡単に聞こえる。

は、しかし、私のLogCatを見た後、私はそれがこれを言っ気づい:

08-12 10:26:50.973: ERROR/AndroidRuntime(1205): FATAL EXCEPTION: main 
08-12 10:26:50.973: ERROR/AndroidRuntime(1205): java.lang.RuntimeException: Unable to start activity ComponentInfo{around.lowell/around.lowell.Main}: java.lang.NullPointerException 
08-12 10:26:50.973: ERROR/AndroidRuntime(1205):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663) 
08-12 10:26:50.973: ERROR/AndroidRuntime(1205):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679) 
08-12 10:26:50.973: ERROR/AndroidRuntime(1205):  at android.app.ActivityThread.access$2300(ActivityThread.java:125) 
08-12 10:26:50.973: ERROR/AndroidRuntime(1205):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033) 
08-12 10:26:50.973: ERROR/AndroidRuntime(1205):  at android.os.Handler.dispatchMessage(Handler.java:99) 
08-12 10:26:50.973: ERROR/AndroidRuntime(1205):  at android.os.Looper.loop(Looper.java:123) 
08-12 10:26:50.973: ERROR/AndroidRuntime(1205):  at android.app.ActivityThread.main(ActivityThread.java:4627) 
08-12 10:26:50.973: ERROR/AndroidRuntime(1205):  at java.lang.reflect.Method.invokeNative(Native Method) 
08-12 10:26:50.973: ERROR/AndroidRuntime(1205):  at java.lang.reflect.Method.invoke(Method.java:521) 
08-12 10:26:50.973: ERROR/AndroidRuntime(1205):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:858) 
08-12 10:26:50.973: ERROR/AndroidRuntime(1205):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) 
08-12 10:26:50.973: ERROR/AndroidRuntime(1205):  at dalvik.system.NativeStart.main(Native Method) 
08-12 10:26:50.973: ERROR/AndroidRuntime(1205): Caused by: java.lang.NullPointerException 
08-12 10:26:50.973: ERROR/AndroidRuntime(1205):  at around.lowell.Main.onCreate(Main.java:22) 
08-12 10:26:50.973: ERROR/AndroidRuntime(1205):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047) 
08-12 10:26:50.973: ERROR/AndroidRuntime(1205):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627) 
08-12 10:26:50.973: ERROR/AndroidRuntime(1205):  ... 11 more 

(「main.xml」はmainselect」につながることが主な活動を開始することはできませんと言っているように見える..whichボタンを押すことで、.xml 'と表示されます)。しかし、私はmain.xmlやMain.javaを変更したことはありません。誰にも私は何を試してみるべきですか?あるいは、誰かがここで問題を見ているのでしょうか?

コード:あなたのMain.javaでのnull参照のため

Main.java ------

package around.lowell; 

import android.app.Activity; 
import android.os.Bundle; 
import android.view.View; 
import android.view.View.OnClickListener; 
import android.widget.CheckBox; 
import android.widget.LinearLayout; 
import android.content.Intent; 

public class Main extends Activity implements OnClickListener { 
// Used for color: 1 = color, 0 = not 
public static int x = 1; 

@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.main); 

    // Set up click listeners for all buttons 
    View v1 = findViewById(R.id.continueButton); 
    v1.setOnClickListener(this); 

    View v2 = findViewById(R.id.colorCheck); 
    v2.setOnClickListener(this); 
} 

public void onClick(View v) { 
    switch (v.getId()) { 
    case R.id.continueButton: 
     Intent i1 = new Intent(this, MainSelect.class); 
     startActivity(i1); 
     break; 
    case R.id.colorCheck: 
     CheckBox check = (CheckBox) findViewById(R.id.colorCheck); 
     if(check.isChecked()) { 
      // Main 
      x = 1; 
      LinearLayout l1a = (LinearLayout) findViewById(R.id.mainLayout); 
      l1a.setBackgroundResource(R.drawable.background); 
      View b1a = findViewById(R.id.continueButton); 
      b1a.setBackgroundResource(R.drawable.buttoncolor); 
      View b2a = findViewById(R.id.colorCheck); 
      b2a.setBackgroundResource(R.drawable.buttoncolor); 
     } else { 
      // Main 
      x = 0; 
      LinearLayout l1a = (LinearLayout) findViewById(R.id.mainLayout); 
      l1a.setBackgroundColor(R.color.blackground); 
      View b1a = findViewById(R.id.continueButton); 
      b1a.setBackgroundResource(R.drawable.colorless); 
      View b2a = findViewById(R.id.colorCheck); 
      b2a.setBackgroundResource(R.drawable.colorless); 
     } 
     break; 
    } 
} 
} 

main.xml ----

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/mainLayout" 
    android:orientation="vertical" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:background="@drawable/background" 
    > 
    <ScrollView 
    android:layout_height="fill_parent" 
    android:layout_width="fill_parent" > 
<LinearLayout 
    android:orientation="vertical" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" > 
    <TextView 
     android:id="@+id/welcomeText1" 
     android:text="Welcome to the..." 
     android:textColor="#FFFFFF" 
     android:textSize="24sp" 
     android:layout_height="wrap_content" 
     android:layout_width="fill_parent" 
     android:layout_marginTop="70dip" 
     android:gravity="center" 
     > 
    </TextView> 
    <TextView 
     android:id="@+id/welcomeText2" 
     android:text="Around Lowell App" 
     android:textColor="#FFFFFF" 
     android:textSize="44sp" 
     android:layout_height="wrap_content" 
     android:layout_width="fill_parent" 
     android:layout_marginTop="30dip" 
     android:gravity="center" 
     > 
    </TextView> 
    <TextView 
     android:id="@+id/author" 
     android:text="By: Mike Stowell" 
     android:textColor="#FFFFFF" 
     android:textSize="12sp" 
     android:layout_height="wrap_content" 
     android:layout_width="fill_parent" 
     android:layout_marginTop="20dip" 
     android:gravity="center" 
    > 
    </TextView> 
    <Button 
     android:id="@+id/continueButton" 
     android:text="Continue" 
     android:textColor="#FFFFFF" 
     android:background="@drawable/buttoncolor" 
     android:layout_height="40sp" 
     android:layout_width="fill_parent" 
     android:layout_marginLeft="30dip" 
     android:layout_marginRight="30dip" 
     android:layout_marginTop="30dip" 
     android:layout_marginBottom="10dip" 
    > 
    </Button> 
    <CheckBox 
     android:id="@+id/colorCheck" 
     android:text="   Color " 
     android:checked="true" 
     android:background="@drawable/buttoncolor" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_gravity="center" 
    > 
    </CheckBox> 
</LinearLayout> 
    </ScrollView> 
</LinearLayout> 
+1

Main.javaの22行目に問題があります。そこにはどのようなコードがありますか? –

+2

ビルディングエラーのように見えます。 'gen'フォルダを再生成するためにプロジェクトをクリーン/ビルドしてみてください。 – ernazm

+0

Genius!それは清潔に働いた!答えとして投稿すると、私は小さな緑色のチェックボックスを付けます:)。編集:または私はそれを自分で答えますが、私はあなたのためにそれを信用します。ありがとうございました! EDIT2:それは明らかに私は8時間自分自身に答えることはできません。私は後でそれを得るでしょう。 – Mxyk

答えて

1

このNPEの最初の考えられる理由は、Pompe de veloが指し示すものです。でる。 2つ目はビルディングエラーです。何とかEclipseプラグイン(またはアンドロイド構築ツール自体)は時々間違ったRを生成して、NPEやビュー/レイアウト/文字列などのメッセージを指し示すことがあります。
これは、私たちがcontinueButtonを見ることができる2番目の種類のcozのちょっとですmain.xmlのビューです。

3

チェックファイル、行22に表示されます。

+0

21行目と22行目は、 'View v1 = findViewById(R.id.continueButton);'と 'v1.setOnClickListener(this);'です。私はcontinueButtonを編集したことはありません。 – Mxyk

+0

したがって、v1はnullです。現在のレイアウトにcontinueButton IDがあるかどうかを調べます。 –

+0

ビュー 'v1'はあなたの 'findViewById()'関数からのヌル参照を取得しています。したがって、 'setOnClickListener()'を呼び出すことはできません。レイアウトXMLファイルに 'continueButton'というIDがあることを確認します。このXMLファイルは、' setContentView() '関数に渡すものです。 – Wroclai

関連する問題