Java 意图不起作用,单击按钮2,应用程序崩溃
我无法在活动之间切换,当我单击按钮2时,应用程序崩溃Java 意图不起作用,单击按钮2,应用程序崩溃,java,android,android-layout,Java,Android,Android Layout,我无法在活动之间切换,当我单击按钮2时,应用程序崩溃 <pre>package com.example.againtry; import java.util.Random; import android.app.Activity; import android.content.Intent; import android.os.Bundle; import android.view.Menu; import android.view
<pre>package com.example.againtry;
import java.util.Random;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class MainActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Declare our View Variables and assign them the Views from the layout file
final TextView answerLabel = (TextView) findViewById(R.id.textView1);
Button getAnswerButton = (Button) findViewById(R.id.button1);
getAnswerButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
// The Button was clicked, so the update the answer label with an answer
String answer = "";
// Randomly select one of three answers: Yes, No or May be
// Update the label with our dynamic answer
Random randomGenerator = new Random(); // Construct a new Random number Generator
int randomNumber = randomGenerator.nextInt(3);
answer = Integer.toString(randomNumber);
// update the label with our dynamic answer
answerLabel.setText(answer);
}});
Button btnSimple= (Button) findViewById(R.id.button2);
btnSimple.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// From One Activity to another
Intent intent = new Intent(MainActivity.this, SendMessageActivity.class);
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;
}}</pre>
MainActivity.Java
这是我的第二项活动
Button getSendButton = (Button) findViewById(R.id.button1);
在SendMessageActivity(即send_message.xml)的布局中,没有id为button1的按钮。
因此,在这样做时:
findViewById返回null,因此getSendButton.setOnClickListener/***/;抛出NullPointerException
应该是:
您必须将onclick分配给一个特定的按钮,我在activity\u主xml中没有看到这个按钮 只需将onclick事件添加到按钮并删除onclicklistener,它就应该可以工作了
要添加onclick侦听器,您可以转到designer页面,在properties中找到标记onclick。只需使用下拉菜单将onlick方法分配给该标记。SendMessageActivity中的控件映射有错误
不是按钮1,而是R.id.sendButton请张贴stacktrace。
Button getSendButton = (Button) findViewById(R.id.button1);
Button getSendButton = (Button) findViewById(R.id.sendButton);
public class SendMessageActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.send_message);
final EditText editSMS = (EditText) findViewById(R.id.editText1);
final EditText editPhoneNum = (EditText) findViewById(R.id.phoneNum);
Button getSendButton = (Button) findViewById(R.id.button1);