单击按钮以继续执行其他活动时,Android应用程序强制将关闭

时间:2012-05-31 14:18:02

标签: java android android-intent android-activity forceclose

我正在使用android 2.2和eclipse制作一个Android应用程序。 它是一个简单的应用程序,应该将活动从“CoverPageApp.java”更改为“LoginActivity.java” 然后到另一个活动。 但是,当我单击CoverPageApp.java中的“开始按钮”时,应用程序强制关闭。

LoginActivity.java

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


public class LoginActivity extends Activity {

    Button btnLinkToRegistrScrn;
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
     setContentView(R.layout.login);
     addListenerOnButton();
     btnLinkToRegistrScrn = (Button) findViewById(R.id.LinkToRegisterScreen); 

   }


    public void addListenerOnButton() {

        final Context context2 = this;

        Button loginbtn1 = (Button) findViewById(R.id.btnLogin);

            loginbtn1.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View arg0) {

                Intent intent = new Intent(context2, DashboardActivity.class);
                         context2.startActivity(intent);   
            }

        });
    }


    { 
        // Link to Register Screen 
        btnLinkToRegistrScrn.setOnClickListener(new View.OnClickListener() { 
           @Override
            public void onClick(View view) { 
                Intent i = new Intent(getApplicationContext(), CAActivity.class); 
                startActivity(i); 
                } 
       }); 

    }   

   }

logcat的

05-31 12:37:08.620: D/AndroidRuntime(629): Shutting down VM
05-31 12:37:08.620: W/dalvikvm(629): threadid=1: thread exiting with uncaught exception (group=0x4001d800)
05-31 12:37:08.630: E/AndroidRuntime(629): FATAL EXCEPTION: main
05-31 12:37:08.630: E/AndroidRuntime(629):  at android.app.Instrumentation.checkStartActivityResult(Instrumentation.java:1404)
05-31 12:37:08.630: E/AndroidRuntime(629):  at android.app.Instrumentation.execStartActivity(Instrumentation.java:1378)
05-31 12:37:08.630: E/AndroidRuntime(629):  at android.app.Activity.startActivityForResult(Activity.java:2817)
05-31 12:37:08.630: E/AndroidRuntime(629):  at android.app.Activity.startActivity(Activity.java:2923)
05-31 12:37:08.630: E/AndroidRuntime(629):  at android.view.View.performClick(View.java:2408)
05-31 12:37:08.630: E/AndroidRuntime(629):  at android.view.View$PerformClick.run(View.java:8816)
05-31 12:37:08.630: E/AndroidRuntime(629):  at android.os.Handler.handleCallback(Handler.java:587)
05-31 12:37:08.630: E/AndroidRuntime(629):  at android.os.Handler.dispatchMessage(Handler.java:92)
05-31 12:37:08.630: E/AndroidRuntime(629):  at android.os.Looper.loop(Looper.java:123)
05-31 12:37:08.630: E/AndroidRuntime(629):  at android.app.ActivityThread.main(ActivityThread.java:4627)
05-31 12:37:08.630: E/AndroidRuntime(629):  at java.lang.reflect.Method.invokeNative(Native Method)
05-31 12:37:08.630: E/AndroidRuntime(629):  at java.lang.reflect.Method.invoke(Method.java:521)
05-31 12:37:08.630: E/AndroidRuntime(629):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
05-31 12:37:08.630: E/AndroidRuntime(629):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
05-31 12:37:08.630: E/AndroidRuntime(629):  at dalvik.system.NativeStart.main(Native Method)

2 个答案:

答案 0 :(得分:2)

请查看以下内容:

打开您的清单文件:

enter image description here

现在添加活动:

enter image description here

enter image description here

enter image description here

保存您的清单文件并再次运行您的项目。

现在您的manifest.xml如下所示:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.test_new"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk android:minSdkVersion="3" />

    <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name" >
        <activity
            android:label="@string/app_name"
            android:name=".Test_newActivity" >
            <intent-filter >
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity android:name=".LoginActivity"></activity>
    </application>

</manifest>

答案 1 :(得分:1)

我认为您尚未添加活动

LoginActivity.java

在Android清单文件中 只需将以下代码添加到AndroidManifest.xml文件

即可
<application>
        <activity
            android:name=".LoginActivity"
            android:label="@string/login_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.HOME" />
            </intent-filter>
        </activity>
    </application>