Java 点击按钮后应用程序崩溃
我正在学习谷歌的android开发教程。我刚刚开始,但我遇到了一个问题,似乎无法解决它。 每当我点击“发送”按钮,我的应用程序就会崩溃,而不是打开新活动 我的初始家庭活动的XML:Java 点击按钮后应用程序崩溃,java,android,xml,Java,Android,Xml,我正在学习谷歌的android开发教程。我刚刚开始,但我遇到了一个问题,似乎无法解决它。 每当我点击“发送”按钮,我的应用程序就会崩溃,而不是打开新活动 我的初始家庭活动的XML: </LinearLayout> <EditText android:id="@+id/edit_message" android:layout_weight="1" android:layout_width="0dp" android:layout_heig
</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>
以及新活动的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:无法启动活动
ComponentInfo{appfactree.me.dotnotes/appfactree.me.dotnotes.DisplayMessageActivity}:
java.lang.NullPointerException:尝试调用虚拟方法
'java.lang.CharSequence android.support.v7.widget.Toolbar.getTitle()'
关于空对象引用
如果你还需要什么,请问一下。
非常感谢你的帮助
编辑:根据请求,完整的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);
}
}
你的直线布局有点乱。。。您有一个打开LinearLayout的结束标记。用户,而不是第一行上的开始标记
这不是问题的原因。这似乎与你的工具栏有关。。。但是,无法从您发布的代码中准确解读内容。发布您的DisplayMessageActivity.java代码问题很可能存在于您的DisplayMessageActivity.class再次阅读错误,您需要解决问题的所有内容都在其中。(另外,您的错误不在发布的代码中)我已根据请求添加了DisplayMessageActivity.java代码。我已发布了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);
}
}