0

{我知道已经有一大堆这样的问题,但我看不到任何与我的程序相关的问题。}

我有一个 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);
            }

        }

    });
}

}

姓名标签活动:

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>

4

1 回答 1

0

原来我不小心切换了 NameTag Activity 和 AndroidNameTag Activity 的布局,并在错误的 Activity 中删除了第二个 TextView...

于 2013-10-26T16:20:14.303 回答