进行另一项活动时Android应用程序强制关闭

时间:2012-06-05 06:06:33

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

我正在使用android 2.2和eclipse制作一个Android应用程序。

该应用有两个工作流程:

WF1: CoverPageApp -> LoginActivity -> Dashboard.

WF2: CoverPageApp -> RegisterActivity -> Dashboard.

但是当我点击CoverPageApp中的“开始”按钮继续进行另一项活动,即LoginActivity时,app force将关闭。 我还包括显示Null Exception错误的LogCat,以及它在第51行指向的LoginActivity Java File:  btnLinkToRegistrScrn = (Button) findViewById(R.id.LinkToRegisterScreen);

的AndroidManifest.xml

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

    <uses-sdk android:targetSdkVersion="15" android:minSdkVersion="8" />

    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
    <uses-permission android:name="android.permission.INTERNET" />

    <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name" >

        <activity
            android:enabled="true"
            android:name=".PageApp"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

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

        <activity 
            android:enabled="true"
            android:name=".LoginActivity1"
            android:label="Login Activity" >
        </activity>

        <activity
            android:enabled="true"
            android:name=".CAActivity"
            android:label="Register Activity" >
        </activity>

        <activity
            android:enabled="true"
            android:name=".DashboardActivity"
            android:label="Dashboard Activity" >
        </activity>

    </application>

  </manifest>

PageApp.java

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

public class PageApp extends Activity {

    Button startbutton;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.coverpage);
        addListenerOnButton();
   }


    public void addListenerOnButton() {

        //final Context context1 = this;

        startbutton = (Button) findViewById(R.id.button1);

        startbutton.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View arg0) {
                Intent intent1 = new Intent(arg0.getContext(), LoginActivity1.class); 
            //    intent1.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                startActivity(intent1); 
                finish();
                }
            });

    }

}

Log.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 Log extends Activity {

    Button btnLinkToRegistrScrn;
    Button loginbtn1;

    /** 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;

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

            loginbtn1.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View arg0) {

                Intent intent = new Intent(arg0.getContext(), DashboardActivity.class);
                intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                         startActivity(intent);   
            }

        });
    }


    { 
        // Link to Register Screen 
        btnLinkToRegistrScrn = (Button) findViewById(R.id.LinkToRegisterScreen); 
        btnLinkToRegistrScrn.setOnClickListener(new View.OnClickListener() { 
           @Override
            public void onClick(View v) { 
                Intent i = new Intent(v.getContext(), CAaactivity.class); 
              //  i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                startActivity(i);
                } 
       }); 

    }   

   }

logcat的

05-31 17:53:19.691: D/AndroidRuntime(1958): Shutting down VM
05-31 17:53:19.710: W/dalvikvm(1958): threadid=1: thread exiting with uncaught exception (group=0x4001d800)
05-31 17:53:19.730: E/AndroidRuntime(1958): FATAL EXCEPTION: main
05-31 17:53:19.730: E/AndroidRuntime(1958):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2585)
05-31 17:53:19.730: E/AndroidRuntime(1958):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
05-31 17:53:19.730: E/AndroidRuntime(1958):     at android.app.ActivityThread.access$2300(ActivityThread.java:125)
05-31 17:53:19.730: E/AndroidRuntime(1958):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
05-31 17:53:19.730: E/AndroidRuntime(1958):     at android.os.Handler.dispatchMessage(Handler.java:99)
05-31 17:53:19.730: E/AndroidRuntime(1958):     at android.os.Looper.loop(Looper.java:123)
05-31 17:53:19.730: E/AndroidRuntime(1958):     at android.app.ActivityThread.main(ActivityThread.java:4627)
05-31 17:53:19.730: E/AndroidRuntime(1958):     at java.lang.reflect.Method.invokeNative(Native Method)
05-31 17:53:19.730: E/AndroidRuntime(1958):     at java.lang.reflect.Method.invoke(Method.java:521)
05-31 17:53:19.730: E/AndroidRuntime(1958):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
05-31 17:53:19.730: E/AndroidRuntime(1958):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
05-31 17:53:19.730: E/AndroidRuntime(1958):     at dalvik.system.NativeStart.main(Native Method)
05-31 17:53:19.730: E/AndroidRuntime(1958): Caused by: java.lang.NullPointerException
05-31 17:53:19.730: E/AndroidRuntime(1958):     at android.app.Activity.findViewById\untime(1958):  at java.lang.Class.newInstanceImpl(Native Method)
05-31 17:53:19.730: E/AndroidRuntime(1958):     at java.lang.Class.newInstance(Class.java:1429)
05-31 17:53:19.730: E/AndroidRuntime(1958):     at android.app.Instrumentation.newActivity(Instrumentation.java:1021)
05-31 17:53:19.730: E/AndroidRuntime(1958):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2577)
05-31 17:53:19.730: E/AndroidRuntime(1958):     ... 11 more

4 个答案:

答案 0 :(得分:2)

在您声明的Login.xml中

<TextView
    android:id="@+id/LinkToRegisterScreen"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"/>

并映射LoginActivity1.java

 btnLinkToRegistrScrn = (Button) findViewById(R.id.LinkToRegisterScreen); 

所以只需将TextView更改为Button

即可
<Button
    android:id="@+id/LinkToRegisterScreen"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"/>

答案 1 :(得分:1)

LoginActivity1 活动更改为:

public class LoginActivity1 extends Activity {

    Button btnLinkToRegistrScrn;
    Button loginbtn1;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
     setContentView(R.layout.login);
     addListenerOnButton();
        // Link to Register Screen 
        btnLinkToRegistrScrn = (Button) findViewById(R.id.LinkToRegisterScreen); 
        btnLinkToRegistrScrn.setOnClickListener(new View.OnClickListener() { 
           @Override
            public void onClick(View v) { 
                Intent i = new Intent(v.getContext(), TrekEyesAndroidActivity.class); 
              //  i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                startActivity(i);
                } 
       }); 
   }
    public void addListenerOnButton() {

        //final Context context2 = this;

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

            loginbtn1.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View arg0) {

                Intent intent = new Intent(arg0.getContext(), DashboardActivity.class);
                intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                         startActivity(intent);   
            }

        });
    }
   }

并在xml中将 TextView 更改为按钮

<Button
         android:id="@+id/LinkToRegisterScreen"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:layout_marginTop="25dip"

            android:textColor="#21dbd4"
            android:textStyle="bold" 
            android:text="@string/noAccountRegisterME" />  

答案 2 :(得分:1)

在您的login.xml中

<TextView
            android:id="@+id/LinkToRegisterScreen"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:layout_marginTop="25dip"

            android:textColor="#21dbd4"
            android:textStyle="bold" 
            android:text="@string/noAccountRegisterME" />

android:id="@+id/LinkToRegisterScreen"它是EditText而你正在做btnLinkToLoginScrn = (Button) findViewById(R.id.LinkToLoginScreen);因此它将是空指针..

所以只需在login.xml中更改Button而不是TextView

答案 3 :(得分:0)

我认为你应该集中精力。你给代码添加listeneraddListenerOnButton()