{我知道已经有很多像这样的问题,但我看不出任何与我的程序有关的问题。}
我有一个Android应用程序应该从EditText中取名并将其放在另一个活动的TextView中。它以前工作,但如果EditText的值等于“ANDROID”,我希望它启动另一个活动。但是,现在每当我尝试启动任一活动时,应用程序都会崩溃。
第一项活动:
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活动:
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活动:
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错误:
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布局:
<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>
答案 0 :(得分:0)
事实证明我意外地切换了名称标签活动和Android名称标签活动的布局,并删除了错误活动中的第二个TextView ...