gpt4 book ai didi

java - Intent 不起作用,单击按钮2时,应用程序崩溃

转载 作者:行者123 更新时间:2023-12-01 13:48:27 27 4
gpt4 key购买 nike

我无法在 Activity 之间切换,当我单击 button2 时,应用程序崩溃了。

MainActivity.Java

<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>

这是我的第二个 Activity

SendMessageActivity.Java
<pre>package com.example.againtry;

import android.app.Activity;
import android.os.Bundle;
import android.telephony.SmsManager;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

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



getSendButton.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View v) {
String phoneNo = editPhoneNum.getText().toString();
String sms = editSMS.getText().toString();

try {
SmsManager smsManager = SmsManager.getDefault();
smsManager.sendTextMessage(phoneNo, null, sms, null, null);
Toast.makeText(getApplicationContext(), "SMS Sent!",
Toast.LENGTH_LONG).show();
} catch (Exception e) {
Toast.makeText(getApplicationContext(),
"SMS faild, please try again later!",
Toast.LENGTH_LONG).show();
e.printStackTrace();
}

}
});

}


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


Manifest
<pre> <?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.againtry"
android:versionCode="1"
android:versionName="1.0" >

<uses-sdk
android:minSdkVersion="10"
android:targetSdkVersion="10" />
<uses-permission android:maxSdkVersion="10" android:name="android.permission.READ_CONTACTS"/>
<uses-permission android:maxSdkVersion="10" android:name="android.permission.SEND_SMS"/>
<uses-permission android:maxSdkVersion="10" android:name="android.permission.READ_SMS"/>

<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.example.againtry.MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name="com.example.againtry.SendMessageActivity"
android:label="@string/title_activity_send_message" >
</activity>
</application>

</manifest></pre>

activity_main<pre>

<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:gravity="bottom"
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=".MainActivity" >

<Button
android:id="@+id/button1"
style="@android:style/ButtonBar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/textView1"
android:layout_centerHorizontal="true"
android:layout_marginBottom="70dp"
android:text="Enlighten Me!" />

<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="177dp"
android:textSize="32sp" />

<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@+id/button1"
android:layout_alignLeft="@+id/button1"
android:layout_marginBottom="44dp"
android:text="Message" />

</RelativeLayout></pre>

send_message.xml<pre>

<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=".SendMessageActivity" >

<EditText
android:id="@+id/editText1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:ems="10"
android:hint="Enter your message" >

<requestFocus />
</EditText>

<Button
android:id="@+id/sendButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/editText1"
android:layout_alignBottom="@+id/editText1"
android:layout_alignParentRight="true"
android:text="@string/button_send" />

<TextView
android:id="@+id/messageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/editText1"
android:layout_alignParentTop="true"
android:layout_marginTop="77dp"
android:text="TextView" />

<EditText
android:id="@+id/phoneNum"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/messageView1"
android:layout_alignParentTop="true"
android:layout_marginTop="18dp"
android:text="Enter Phone Number" android:inputType="number"/>
</RelativeLayout></pre>

最佳答案

SendMessageActivity 的布局(即 send_message.xml)中,没有 ID 为“button1”的按钮。所以在做的时候:

Button getSendButton = (Button) findViewById(R.id.button1);

findViewById 返回 null,因此 getSendButton.setOnClickListener(/***/); 抛出 NullPointerException .

应该是:

Button getSendButton = (Button) findViewById(R.id.sendButton);

关于java - Intent 不起作用,单击按钮2时,应用程序崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20162706/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com