Unable to Start Activity ComponentInfo when Starting a New Activity
Posted
by
Timtim17
on Stack Overflow
See other posts from Stack Overflow
or by Timtim17
Published on 2013-10-26T15:58:33Z
Indexed on
2013/11/01
21:54 UTC
Read the original article
Hit count: 381
android
{I know there's already a whole bunch of questions like this, but I can't see any problems that related to my program.}
I have an Android App that is supposed to take a name from a EditText and put it in a TextView in another activity. It previously worked, but then I wanted it to start another activity if the EditText's value was equal to "ANDROID". However, now the app crashes whenever I try to start either activity.
First Activity:
package net.timtim17.dev.android.fun.nametag;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final EditText et = (EditText) findViewById(R.id.editText1);
Button submit = (Button) findViewById(R.id.button1);
submit.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View v) {
String text = et.getText().toString();
if(text.equals("ANDROID")){
Intent android = new Intent(MainActivity.this, AndroidNameTag.class);
startActivity(android);
}else{
Intent intent = new Intent(MainActivity.this, NameTag.class);
intent.putExtra("name", text);
startActivity(intent);
}
}
});
}
}
NameTag Activity:
package net.timtim17.dev.android.fun.nametag;
import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;
public class NameTag extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_tag);
TextView tv = (TextView) findViewById(R.id.textView2);
tv.setText(getIntent().getExtras().getString("name"));
}
}
AndroidNameTag Activity:
package net.timtim17.dev.android.fun.nametag;
import android.app.Activity;
import android.graphics.drawable.AnimationDrawable;
import android.os.Bundle;
import android.widget.ImageView;
public class AndroidNameTag extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_android);
ImageView iv = (ImageView) findViewById(R.id.imageView1);
iv.setBackgroundResource(R.anim.animation);
AnimationDrawable anim = (AnimationDrawable) iv.getBackground();
anim.start();
}
}
LogCat Error:
10-26 11:26:35.602: E/AndroidRuntime(2900): FATAL EXCEPTION: main
10-26 11:26:35.602: E/AndroidRuntime(2900): java.lang.RuntimeException: Unable to start activity ComponentInfo{net.timtim17.dev.android.fun.nametag/net.timtim17.dev.android.fun.nametag.NameTag}: java.lang.NullPointerException
10-26 11:26:35.602: E/AndroidRuntime(2900): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2211)
10-26 11:26:35.602: E/AndroidRuntime(2900): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2261)
10-26 11:26:35.602: E/AndroidRuntime(2900): at android.app.ActivityThread.access$600(ActivityThread.java:141)
10-26 11:26:35.602: E/AndroidRuntime(2900): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1256)
10-26 11:26:35.602: E/AndroidRuntime(2900): at android.os.Handler.dispatchMessage(Handler.java:99)
10-26 11:26:35.602: E/AndroidRuntime(2900): at android.os.Looper.loop(Looper.java:137)
10-26 11:26:35.602: E/AndroidRuntime(2900): at android.app.ActivityThread.main(ActivityThread.java:5103)
10-26 11:26:35.602: E/AndroidRuntime(2900): at java.lang.reflect.Method.invokeNative(Native Method)
10-26 11:26:35.602: E/AndroidRuntime(2900): at java.lang.reflect.Method.invoke(Method.java:525)
10-26 11:26:35.602: E/AndroidRuntime(2900): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
10-26 11:26:35.602: E/AndroidRuntime(2900): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
10-26 11:26:35.602: E/AndroidRuntime(2900): at dalvik.system.NativeStart.main(Native Method)
10-26 11:26:35.602: E/AndroidRuntime(2900): Caused by: java.lang.NullPointerException
10-26 11:26:35.602: E/AndroidRuntime(2900): at net.timtim17.dev.android.fun.nametag.NameTag.onCreate(NameTag.java:15)
10-26 11:26:35.602: E/AndroidRuntime(2900): at android.app.Activity.performCreate(Activity.java:5133)
10-26 11:26:35.602: E/AndroidRuntime(2900): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
10-26 11:26:35.602: E/AndroidRuntime(2900): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2175)
10-26 11:26:35.602: E/AndroidRuntime(2900): ... 11 more
MainActivity Layout:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<TextView
android:id="@+id/textView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginTop="16dp"
android:text="@string/main_text"
android:textAppearance="?android:attr/textAppearanceMedium" />
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignRight="@+id/textView1"
android:layout_below="@+id/textView1"
android:layout_marginTop="14dp"
android:text="@string/submit_button" />
<EditText
android:id="@+id/editText1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/textView1"
android:layout_alignTop="@+id/button1"
android:ems="10"
android:inputType="textPersonName" >
<requestFocus />
</EditText>
© Stack Overflow or respective owner