0

我有问题。我创建了一个从资源中加载随机布局的应用程序,但我遇到了问题,因为程序显示随机布局并关闭,有时在 2 次之后,有时在 12 次之后,我不知道问题出在哪里,我试图在我的手机上运行它并且我在清单中添加了 write_external_storage 权限,但仍然是同样的错误。请帮助我,并尝试解决此问题。

这是我的代码:

爪哇:

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

安卓清单:

<?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
4

2 回答 2

0

其中一个 idR.image.imageX丢失了。它们可能拼写错误,或者您可能错过了命名它们,因此请检查它们。

您也可能指的是未在 main.xml 中声明的 id(此活动中引用的 xml 文件setContentView(R.layout.main);)。

希望你让它工作。:-)

于 2012-07-10T16:20:22.023 回答
0

您可能在布局文件中的某处缺少 id,这导致在单击方法中找不到按钮之一。如果您可以发布您的布局 xml 文件,我们可以告诉您确切的位置。

于 2012-07-10T15:16:39.203 回答