問題があります。リソースからランダムなレイアウトをロードするアプリケーションを作成しましたが、プログラムがランダムなレイアウトを示して終了するため、問題が発生します。2回後、場合によっては12回後に終了します。どこに問題があるのかわからないので、携帯電話で実行してみました。マニフェストのwrite_external_storage権限を追加しましたが、それでも同じエラーが発生します。助けて、この問題の解決を試みてください。
これが私のコードです:
java:
package ka.ka.ka;
import java.util.Random;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class KAMASActivity extends Activity implements OnClickListener {
Button button1;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
button1 = (Button) findViewById(R.id.button1) ;
button1.setOnClickListener(this);
}
@Override
public void onClick(View v) {
int min = 1;
int max = 6;
int i1=0;
Random r = new Random();
i1 = r.nextInt(max - min + 1) + min;
if(i1==1){setContentView(R.layout.image1);
button1 = (Button) findViewById(R.id.button1) ;
button1.setOnClickListener(this);}
if(i1==2){setContentView(R.layout.image2);
button1 = (Button) findViewById(R.id.button1) ;
button1.setOnClickListener(this);}
if(i1==3){setContentView(R.layout.image3);
button1 = (Button) findViewById(R.id.button1) ;
button1.setOnClickListener(this);}
if(i1==4){setContentView(R.layout.image4);
button1 = (Button) findViewById(R.id.button1) ;
button1.setOnClickListener(this);}
if(i1==5){setContentView(R.layout.image5);
button1 = (Button) findViewById(R.id.button1) ;
button1.setOnClickListener(this);}
if(i1==6){setContentView(R.layout.image6);
button1 = (Button) findViewById(R.id.button1) ;
button1.setOnClickListener(this);
}
}
Androidマニフェスト:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="ka.ka.ka"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk android:minSdkVersion="7" />
<uses-permission android:name="android.permission.ACCESS_CHECKIN_PROPERTIES" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<application
android:icon="@drawable/ikona"
android:label="@string/app_name" >
<activity
android:name=".KAMASActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
そして彼はlogcatです:
07-10 10:58:51.062: D/ddm-heap(218): Got feature list request
07-10 10:58:51.311: D/dalvikvm(218): GC freed 506 objects / 46032 bytes in 122ms
07-10 10:59:30.081: D/AndroidRuntime(218): Shutting down VM
07-10 10:59:30.081: W/dalvikvm(218): threadid=3: thread exiting with uncaught exception (group=0x4001b188)
07-10 10:59:30.081: E/AndroidRuntime(218): Uncaught handler: thread main exiting due to uncaught exception
07-10 10:59:30.102: E/AndroidRuntime(218): java.lang.NullPointerException
07-10 10:59:30.102: E/AndroidRuntime(218): at ka.ka.ka.KAMASActivity.onClick(KAMASActivity.java:32)
07-10 10:59:30.102: E/AndroidRuntime(218): at android.view.View.performClick(View.java:2364)
07-10 10:59:30.102: E/AndroidRuntime(218): at android.view.View.onTouchEvent(View.java:4179)
07-10 10:59:30.102: E/AndroidRuntime(218): at android.widget.TextView.onTouchEvent(TextView.java:6541)
07-10 10:59:30.102: E/AndroidRuntime(218): at android.view.View.dispatchTouchEvent(View.java:3709)
07-10 10:59:30.102: E/AndroidRuntime(218): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:884)
07-10 10:59:30.102: E/AndroidRuntime(218): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:884)
07-10 10:59:30.102: E/AndroidRuntime(218): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:884)
0 7-10 10:59:30.102: E/AndroidRuntime(218): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:884)
07-10 10:59:30.102: E/AndroidRuntime(218): at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchTouchEvent(PhoneWindow. java:1659)
07-10 10:59:30.102: E/AndroidRuntime(218): at com.android.internal.policy.impl.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1107)
07-10 10:59:30.102: E/AndroidRuntime(218): at android.app.Activity.dispatchTouchEvent(Activity.java:2061)
07-10 10:59:30.102: E/AndroidRuntime(218): at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.java:1643)
07-10 10:59:30.102: E/AndroidRuntime(218): at android.view.ViewRoot.handleMessage(ViewRoot.java:1691)
07-10 10:59:30.102: E/AndroidRuntime(218): at android.os.Handler.dispatchMessage(Handler.java:99)
07-10 10:59:30.102: E/AndroidRuntime(218): at android.os.Looper.loop(Looper.java:123)
07-10 10:59:30.102: E/AndroidRuntime(218): at android.app.ActivityThread.main(ActivityThread.java:4363)
07-10 10:59:30.102: E/AndroidRuntime(218): at java.lang.reflect.Method.invokeNative(Native Method)
07-10 10:59:30.102: E/AndroidRuntime(218): at java.lang.reflect.Method.invoke(Method.java:521)
07-10 10:59:30.102: E/AndroidRuntime(218): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860)
07-10 10:59:30.102: E/AndroidRuntime(218): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
07-10 10:59:30.102: E/AndroidRuntime(218): at dalvik.system.NativeStart.main(Native Method)
07-10 10:59:30.121: I/dalvikvm(218): threadid=7: reacting to signal 3
07-10 10:59:30.121: E/dalvikvm(218): Unable to open stack trace file '/data/anr/traces.txt': Permission denied
07-10 10:59:32.562: I/Process(218): Sending signal. PID: 218 SIG: 9