I am making an android app using android 2.2 and eclipse.
There are two workflows of the app:
WF1: CoverPageApp -> LoginActivity -> Dashboard.
WF2: CoverPageApp -> RegisterActivity -> Dashboard.
But as I click on the Start Button in CoverPageApp to go on another activity, i.e LoginActivity, the app force closes.
I have also included the LogCat which shows error of Null Exception and in the LoginActivity Java File it points on the line 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
Change Your LoginActivity1 Activity as:
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);
}
});
}
}
and in xml change TextView to Button as
<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" />
in Login.xml you declared
<TextView
android:id="@+id/LinkToRegisterScreen"
android:layout_width="fill_parent"
android:layout_height="wrap_content"/>
and while mapping LoginActivity1.java
btnLinkToRegistrScrn = (Button) findViewById(R.id.LinkToRegisterScreen);
so just change the TextView
to Button
<Button
android:id="@+id/LinkToRegisterScreen"
android:layout_width="fill_parent"
android:layout_height="wrap_content"/>
In your 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"
and it is EditText and you are doing btnLinkToLoginScrn = (Button) findViewById(R.id.LinkToLoginScreen);
so it would be null pointer..
so just change Button instead of TextView in your login.xml
i think u should concentrate the braces.you give the code to add listeneraddListenerOnButton()
来源:https://stackoverflow.com/questions/10892398/android-app-force-closes-while-going-on-another-activity