android活动无缘无故崩溃?也许是因为setcontentview?

时间:2014-06-19 15:35:25

标签: android-activity crash

我遇到了android开发问题(我是新手btw)..

我正在尝试完成本教程:http://developer.android.com/training/basics/firstapp/starting-activity.html,除了我的问题是第二个活动系统崩溃而且我不知道为什么......

当我评论该行时:setContentView(textView1):第二个活动没有崩溃,但文本没有显示。所以我想这就是我的问题所在,但我一直在寻找几个小时,我似乎无法弄明白:-s

这是我的代码:

(主要活动)XML布局:

    <RelativeLayout "
    android:id="@+id/texttoto"
    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.toto.MainActivity$PlaceholderFragment" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/hello_world" />

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_above="@+id/editText_mail"
        android:layout_alignLeft="@+id/editText_mail"
        android:onClick="test_trucs"
        android:text="@string/test" />

    <EditText
        android:id="@+id/txttopass"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/textView1"
        android:layout_below="@+id/textView1"
        android:layout_marginTop="15dp"
        android:ems="10"
        android:hint="@string/texttopass" >
    </EditText>

</RelativeLayout>

** (第二项活动)XML布局: *:*

 <RelativeLayout "
    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.toto.Blabla$PlaceholderFragment" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/hello_world" />

</RelativeLayout>

**** MainActivity.JAVA:****

    package com.toto;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.app.ActionBarActivity;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.Toast;

public class MainActivity extends ActionBarActivity {
    public final static String EXTRA_MESSAGE = "com.toto.MESSAGE";

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

        if (savedInstanceState == null) {
            getSupportFragmentManager().beginTransaction()
                    .add(R.id.container, new PlaceholderFragment())
                    .commit();
        }
    }


    @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);
    }

    /**
     * A placeholder fragment containing a simple view.
     */
    public static class PlaceholderFragment extends Fragment {

        public PlaceholderFragment() {
        }

        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                Bundle savedInstanceState) {
            View rootView = inflater.inflate(R.layout.fragment_main, container, false);
            return rootView;
        }
    }
    /** mettre les class ICI **/
    public void test_trucs(View view) {
        Intent intent = new Intent(this, Blabla.class);
        EditText editText = (EditText) findViewById(R.id.txttopass);
        String message = editText.getText().toString();
        intent.putExtra(EXTRA_MESSAGE, message);
        startActivity(intent);
    }

}

**** Blabla.JAVA:****

package com.toto;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.app.ActionBarActivity;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

public class Blabla extends ActionBarActivity {

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

        if (savedInstanceState == null) {
            getSupportFragmentManager().beginTransaction()
                    .add(R.id.container, new PlaceholderFragment()).commit();
        }

      // Get the message from the intent
      Intent intent = getIntent();
      String message = intent.getStringExtra(MainActivity.EXTRA_MESSAGE);
      // Create the text view
      TextView textView1 = new TextView(this);
      textView1.setTextSize(40);
      textView1.setText(message);

      // Set the text view as the activity layout
      setContentView(textView1);

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {

        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.blabla, 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);
    }

    /**
     * A placeholder fragment containing a simple view.
     */
    public static class PlaceholderFragment extends Fragment {

        public PlaceholderFragment() {
        }

        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                Bundle savedInstanceState) {
            View rootView = inflater.inflate(R.layout.fragment_blabla,
                    container, false);
            return rootView;
        }
    }

}

logcat显示:

06-19 15:35:47.982: E/AndroidRuntime(22378): FATAL EXCEPTION: main
06-19 15:35:47.982: E/AndroidRuntime(22378): Process: com.toto, PID: 22378
06-19 15:35:47.982: E/AndroidRuntime(22378): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.toto/com.toto.Blabla}: java.lang.NullPointerException
06-19 15:35:47.982: E/AndroidRuntime(22378):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2184)
06-19 15:35:47.982: E/AndroidRuntime(22378):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2233)
06-19 15:35:47.982: E/AndroidRuntime(22378):    at android.app.ActivityThread.access$800(ActivityThread.java:135)
06-19 15:35:47.982: E/AndroidRuntime(22378):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
06-19 15:35:47.982: E/AndroidRuntime(22378):    at android.os.Handler.dispatchMessage(Handler.java:102)
06-19 15:35:47.982: E/AndroidRuntime(22378):    at android.os.Looper.loop(Looper.java:136)
06-19 15:35:47.982: E/AndroidRuntime(22378):    at android.app.ActivityThread.main(ActivityThread.java:5001)
06-19 15:35:47.982: E/AndroidRuntime(22378):    at java.lang.reflect.Method.invokeNative(Native Method)
06-19 15:35:47.982: E/AndroidRuntime(22378):    at java.lang.reflect.Method.invoke(Method.java:515)
06-19 15:35:47.982: E/AndroidRuntime(22378):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
06-19 15:35:47.982: E/AndroidRuntime(22378):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
06-19 15:35:47.982: E/AndroidRuntime(22378):    at dalvik.system.NativeStart.main(Native Method)
06-19 15:35:47.982: E/AndroidRuntime(22378): Caused by: java.lang.NullPointerException
06-19 15:35:47.982: E/AndroidRuntime(22378):    at com.toto.Blabla.onCreate(Blabla.java:39)
06-19 15:35:47.982: E/AndroidRuntime(22378):    at android.app.Activity.performCreate(Activity.java:5231)
06-19 15:35:47.982: E/AndroidRuntime(22378):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
06-19 15:35:47.982: E/AndroidRuntime(22378):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2148)
06-19 15:35:47.982: E/AndroidRuntime(22378):    ... 11 more

我知道这可能是有史以来最新的事情,但请放纵......

提前感谢您的帮助!

1 个答案:

答案 0 :(得分:0)

您正在调用setContentView两次,第一次:

setContentView(R.layout.activity_blabla);

不在教程中。我不确定究竟是什么造成了NullPointer异常,但似乎两次调用会导致问题。

如果你想使用更复杂的布局(R.layout_blabla),并显示消息,就像你的情况一样,因为文本视图在那个布局中,通过findViewById从onCreate引用它,而不是创建一个新的TextView

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

    if (savedInstanceState == null) {
        getSupportFragmentManager().beginTransaction()
                .add(R.id.container, new PlaceholderFragment()).commit();
    }

  // Get the message from the intent
  Intent intent = getIntent();
  String message = intent.getStringExtra(MainActivity.EXTRA_MESSAGE);
  // Create the text view
  TextView textView1 = (TextView)findViewById(R.id.textView1)
  textView1.setTextSize(40);
  textView1.setText(message);

  // Set the text view as the activity layout
  setContentView(textView1);

}