Android应用程序在setText上崩溃

时间:2014-08-11 13:51:46

标签: android eclipse

我是Android开发新手,我正在从android的培训网站学习。 在学习"盯着另一个活动"我想为什么不在第二个Activity的XML文件中创建一个textView,并使用.java文件传递我从第一个活动中获得的文本。我做了更改但是当我运行我的应用程序时,它会在我按下按钮时崩溃。这是源代码和错误日志。 请帮帮我。

MainActivity.java:

    public class MainActivity extends Activity {
    public static final String EXTRA_MESSAGE = "com.example.myFirstApplication.MESSAGE";
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }

    public void Input(View view) {
        Intent intent = new Intent(this,MySecondActivity.class);
        EditText editText =  (EditText) findViewById(R.id.inputMessage);
        String message = editText.getText().toString();
        intent.putExtra(EXTRA_MESSAGE, message);
        startActivity(intent);

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }
}

MySecondActivity.java:

    public class MySecondActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        Intent intent = getIntent();
        String message = intent.getStringExtra(MainActivity.EXTRA_MESSAGE);
        TextView text = (TextView) findViewById(R.id.textid);
        text.setTextSize(40);
        text.setText(message);
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_my_second);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }
}

activity_my_second.xml:

<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="com.example.myfirstapplication.MySecondActivity" >

<TextView android:id="@+id/textid"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:layout_centerHorizontal="true"
      android:layout_marginTop="42dp"
      android:textAppearance="?android:attr/textAppearanceLarge"/>
</RelativeLayout>

activity_main.xml:

<LinearLayout 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="com.example.myfirstapplication.MainActivity" >

<EditText android:layout_width="0dp"
          android:layout_weight="1"
          android:layout_height="wrap_content"
          android:id="@+id/inputMessage"
          android:hint="@string/Message"/>

<Button android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/InputButton"
        android:onClick="Input"/>

</LinearLayout>

错误日志:

08-11 19:03:44.285: D/AndroidRuntime(6259): Shutting down VM
08-11 19:03:44.285: W/dalvikvm(6259): threadid=1: thread exiting with uncaught exception (group=0x41dfec08)
08-11 19:03:44.290: E/AndroidRuntime(6259): FATAL EXCEPTION: main
08-11 19:03:44.290: E/AndroidRuntime(6259): Process: com.example.myfirstapplication, PID: 6259
08-11 19:03:44.290: E/AndroidRuntime(6259): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.myfirstapplication/com.example.myfirstapplication.MySecondActivity}: java.lang.NullPointerException
08-11 19:03:44.290: E/AndroidRuntime(6259):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2413)
08-11 19:03:44.290: E/AndroidRuntime(6259):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2471)
08-11 19:03:44.290: E/AndroidRuntime(6259):     at android.app.ActivityThread.access$900(ActivityThread.java:175)
08-11 19:03:44.290: E/AndroidRuntime(6259):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1308)
08-11 19:03:44.290: E/AndroidRuntime(6259):     at android.os.Handler.dispatchMessage(Handler.java:102)
08-11 19:03:44.290: E/AndroidRuntime(6259):     at android.os.Looper.loop(Looper.java:146)
08-11 19:03:44.290: E/AndroidRuntime(6259):     at android.app.ActivityThread.main(ActivityThread.java:5602)
08-11 19:03:44.290: E/AndroidRuntime(6259):     at java.lang.reflect.Method.invokeNative(Native Method)
08-11 19:03:44.290: E/AndroidRuntime(6259):     at java.lang.reflect.Method.invoke(Method.java:515)
08-11 19:03:44.290: E/AndroidRuntime(6259):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1283)
08-11 19:03:44.290: E/AndroidRuntime(6259):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1099)
08-11 19:03:44.290: E/AndroidRuntime(6259):     at dalvik.system.NativeStart.main(Native Method)
08-11 19:03:44.290: E/AndroidRuntime(6259): Caused by: java.lang.NullPointerException
08-11 19:03:44.290: E/AndroidRuntime(6259):     at com.example.myfirstapplication.MySecondActivity.onCreate(MySecondActivity.java:16)
08-11 19:03:44.290: E/AndroidRuntime(6259):     at android.app.Activity.performCreate(Activity.java:5451)
08-11 19:03:44.290: E/AndroidRuntime(6259):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1093)
08-11 19:03:44.290: E/AndroidRuntime(6259):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2377)
08-11 19:03:44.290: E/AndroidRuntime(6259):     ... 11 more

2 个答案:

答案 0 :(得分:8)

您必须先设置内容视图,然后才能找到ViewById,

尝试

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_my_second);

        Intent intent = getIntent();
        String message = intent.getStringExtra(MainActivity.EXTRA_MESSAGE);
        TextView text = (TextView) findViewById(R.id.textid);
        text.setTextSize(40);
        text.setText(message);
    }

答案 1 :(得分:0)

正如布伦德尔所说。确保在膨胀任何窗口小部件之前调用方法setContentView 否则,当您实例化对它们的引用时,您将获得nullpointer excpetion。