2015-12-09 63 views
0

我正在关注Google的android dev教程。我刚刚开始,但我遇到了一个问题,似乎无法解决它。 http://developer.android.com/training/basics/firstapp/starting-activity.html#ReceiveIntent应用程序崩溃按钮点击

每当我点击“发送”按钮,我的应用程序崩溃,而不是打开新的活动。

的XML我最初的家庭活动:

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

    <Button 
    android:id="@+id/button" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:text="@string/button_send" 
    android:onClick="sendMessage"/> 
</LinearLayout> 

该方法的sendMessage在Home.Java文件

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

和Java的新活动

//Get the message from the intent 
    Intent intent = getIntent(); 
    String message = intent.getStringExtra(Home.EXTRA_MESSAGE); 

    //Creating the textview 
    TextView textView = new TextView(this); 
    textView.setTextSize(40); 
    textView.setText(message); 

    //Set text view as activity layout 
    setContentView(textView); 

的主要错误:

java.lang.RuntimeException: Unable to start activity ComponentInfo{appfactree.me.dotnotes/appfactree.me.dotnotes.DisplayMessageActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.CharSequence android.support.v7.widget.Toolbar.getTitle()' on a null object reference

请问你是否需要别的东西。 非常感谢您的帮助。

编辑:按照要求完整DisplayMessageActivity.java代码:

public class DisplayMessageActivity extends AppCompatActivity { 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); 
    setSupportActionBar(toolbar); 

    FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab); 
    fab.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View view) { 
      Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG) 
        .setAction("Action", null).show(); 
     } 
    }); 
    getSupportActionBar().setDisplayHomeAsUpEnabled(true); 

    //Get the message from the intent 
    Intent intent = getIntent(); 
    String message = intent.getStringExtra(Home.EXTRA_MESSAGE); 

    //Creating the textview 
    TextView textView = new TextView(this); 
    textView.setTextSize(40); 
    textView.setText(message); 

    //Set text view as activity layout 
    setContentView(textView); 
    } 
} 
+0

发布您的DisplayMessageActivity.java代码 –

+0

这个问题最有可能发生在您的DisplayMessageActivity.class –

+0

再次读取您的错误,您需要解决您的问题的一切都在那里。 (另外,您的错误不在发布的代码中) – njzk2

回答

0

你的LinearLayout是轻微的塞......你有打开的LinearLayout关闭标签。用户而不是第一行的开始标签。

这不是问题的原因。它似乎与你的工具栏有关......但是无法解释你发布的代码到底是什么。

+0

我已经发布了DisplayMessageActivity.java的代码,希望有所帮助。 – als26