【问题标题】:Android app force closes while going on another activityAndroid 应用程序强制在进行另一项活动时关闭
【发布时间】:2012-06-05 06:06:32
【问题描述】:

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

应用有两种工作流程:

WF1: CoverPageApp -> LoginActivity -> Dashboard.

WF2: CoverPageApp -> RegisterActivity -> Dashboard.

但是当我单击 CoverPageApp 中的开始按钮以进行另一个活动时,即 LoginActivity,应用程序强制关闭。 我还包括了显示空异常错误的 LogCat,在 LoginActivity Java 文件中它指向第 51 行: 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

【问题讨论】:

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


    【解决方案1】:

    在你声明的 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"/>
    

    【讨论】:

    • 谢谢!它解决了一半的问题,但伊姆兰汗的回答解决了整个问题。不管怎么说,还是要谢谢你。 :)
    【解决方案2】:

    将您的 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

    <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" />  
    

    【讨论】:

    • 如果将两个按钮都映射到addListenerOnButton() 会有什么不同?
    • @imran khan 非常感谢 :) 我已经处理这个错误 1 ​​周了,今天你解决了我的应用程序中的这个最大错误。非常感谢 :) 非常感谢您的帮助。现在我的应用程序运行顺利。 :))))))
    • @hotveryspicy : 没问题,他可以在addListenerOnButton() 中映射两个按钮并使用相同的
    【解决方案3】:

    在您的 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

    【讨论】:

    • 不是EditText,而是TextView
    【解决方案4】:

    我认为你应该集中大括号。你给代码添加 listeneraddListenerOnButton()

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-06-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-04-30
      • 2012-12-01
      相关资源
      最近更新 更多