程序错误,强制关闭

时间:2012-07-06 08:12:08

标签: android

我的应用程序有问题,我是初学者,我想创建一个简单的应用程序,从项目中加载随机布局。它工作,但不正确,因为它在启动几次后工作,有时一打,然后它显示有关强制关闭的消息。这是来自eclipse的logCat。我应该更改什么来修复它并使其正常工作?我尝试了很多东西,但关键是我不知道问题出在哪里,这就是为什么我自己无法处理它。 :

07-06 07:27:45.873: D/dalvikvm(220): GC freed 501 objects / 45848 bytes in 122ms
07-06 07:28:50.904: D/dalvikvm(220): GC freed 1436 objects / 65328 bytes in 85ms
07-06 07:28:55.703: D/AndroidRuntime(220): Shutting down VM
07-06 07:28:55.703: W/dalvikvm(220): threadid=3: thread exiting with uncaught exception          (group=0x4001b188)
07-06 07:28:55.703: E/AndroidRuntime(220): Uncaught handler: thread main exiting due to           uncaught exception
07-06 07:28:55.724: E/AndroidRuntime(220): java.lang.RuntimeException: Unable to start      activity ComponentInfo{ka.ka.ka/ka.ka.ka.RANDOMActivity}: java.lang.NullPointerException
07-06 07:28:55.724: E/AndroidRuntime(220):  at   android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2496)
07-06 07:28:55.724: E/AndroidRuntime(220):  at   android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2512)
07-06 07:28:55.724: E/AndroidRuntime(220):  at android.app.ActivityThread.access$2200(ActivityThread.java:119)
07-06 07:28:55.724: E/AndroidRuntime(220):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1863)
07-06 07:28:55.724: E/AndroidRuntime(220):  at android.os.Handler.dispatchMessage(Handler.java:99)
07-06 07:28:55.724: E/AndroidRuntime(220):  at android.os.Looper.loop(Looper.java:123)
07-06 07:28:55.724: E/AndroidRuntime(220):  at android.app.ActivityThread.main(ActivityThread.java:4363)
07-06 07:28:55.724: E/AndroidRuntime(220):  at java.lang.reflect.Method.invokeNative(Native Method)
07-06 07:28:55.724: E/AndroidRuntime(220):  at java.lang.reflect.Method.invoke(Method.java:521)
07-06 07:28:55.724: E/AndroidRuntime(220):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860)
07-06 07:28:55.724: E/AndroidRuntime(220):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
07-06 07:28:55.724: E/AndroidRuntime(220):  at dalvik.system.NativeStart.main(Native Method)
07-06 07:28:55.724: E/AndroidRuntime(220): Caused by: java.lang.NullPointerException
07-06 07:28:55.724: E/AndroidRuntime(220):  at ka.ka.ka.RANDOMActivity.onCreate(RANDOMActivity.java:29)
07-06 07:28:55.724: E/AndroidRuntime(220):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
07-06 07:28:55.724: E/AndroidRuntime(220):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2459)
07-06 07:28:55.724: E/AndroidRuntime(220):  ... 11 more
07-06 07:28:55.754: I/dalvikvm(220): threadid=7: reacting to signal 3
07-06 07:28:55.754: E/dalvikvm(220): Unable to open stack trace file '/data/anr/traces.txt': Permission denied
07-06 07:28:57.283: I/Process(220): Sending signal. PID: 220 SIG: 9

这是我的代码:

KAMASAcctivity:

package ka.ka.ka;

import android.app.Activity;
import android.content.Intent;
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) {

        startActivity(new Intent(getApplicationContext(), RANDOMActivity.class));
        this.finish();
    }

}

RANDOMActivity:

package ka.ka.ka;

import java.util.Random;

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

public class RANDOMActivity extends Activity implements OnClickListener {
    Button button1;
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        int min = 1;
        int max = 4;
        int i1=0;       
        Random r = new Random();
        i1 = r.nextInt(max - min + 1) + min;
        if(i1==1){setContentView(R.layout.image1);}
        if(i1==2){setContentView(R.layout.image2);}
        if(i1==3){setContentView(R.layout.image3);}
        if(i1==4){setContentView(R.layout.image4);}
        button1 = (Button) findViewById(R.id.button1) ;
        button1.setOnClickListener(this);

    }
    @Override
    public void onClick(View v) {


        startActivity(new Intent(getApplicationContext(), RANDOM1Activity.class));
        finish();
    }       
}

RANDOM1Activity:

package ka.ka.ka;

import java.util.Random;

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

public class RANDOM1Activity extends Activity implements OnClickListener {
    Button button1;
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        int min = 1;
        int max = 4;
        int i1=0;       
        Random r = new Random();
        i1 = r.nextInt(max - min + 1) + min;
        if(i1==1){setContentView(R.layout.image1);}
        if(i1==2){setContentView(R.layout.image2);}
        if(i1==3){setContentView(R.layout.image3);}
        if(i1==4){setContentView(R.layout.image4);}
        button1 = (Button) findViewById(R.id.button1) ;
        button1.setOnClickListener(this);

    }
    @Override
    public void onClick(View v) {

        startActivity(new Intent(getApplicationContext(), RANDOMActivity.class));
        finish();
    }
}

2 个答案:

答案 0 :(得分:1)

In your RANDOMActivity suppose if i1 value is not any of the given numbers from 1 to 4, then your activity will not have setContentView(). So you can put an else block to the if conditions and give a default setContentView() like this.

 Random r = new Random();
    i1 = r.nextInt(max - min + 1) + min;
    if(i1==1){setContentView(R.layout.image1);}
else  if(i1==2){setContentView(R.layout.image2);}
else if(i1==3){setContentView(R.layout.image3);}
else if(i1==4){setContentView(R.layout.image4);}
else {
setContentView(R.layout.image4);
}

答案 1 :(得分:0)

从LogCat看到这一行:

Unable to open stack trace file '/data/anr/traces.txt': Permission denied

这意味着您尝试访问traces.txt而未声明AndroidManifest.file中的 READ 权限。