我正在尝试使用意图从一个活动切换到另一个活动,第一个活动运行正常,但是当第二个活动启动时,应用程序停止。我已经附上了这个代码。
显现 :
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.sampleapp"
android:versionCode="1"
android:versionName="1.0" >
...
<application
...
<activity
android:name="com.example.sampleapp.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.sampleapp.welwithname"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.WELWITHNAME" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
...
</application>
</manifest>
这是默认活动。我试图获取字符串并将其发送到下一个活动。这里是 xml 和活动。 活动代码:
public class MainActivity extends Activity {
public final static String EXTRA_MESSAGE = "com.example.sampleapp.MESSAGE";
String tempname;
Button submit;
EditText name;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
submit= (Button)findViewById(R.id.button1);
submit.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Intent intent = new Intent(MainActivity.this,Welwithname.class);
EditText editText = (EditText) findViewById(R.id.getname);
String message = editText.getText().toString();
intent.putExtra(EXTRA_MESSAGE, message);
startActivity(intent);
}
});
}
}
第二个活动:
此活动从活动 1 获取字符串并打印值..这里是 xml 和活动 活动代码:
public class Welwithname extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.welwithname);
Intent intent = getIntent();
String message = intent.getStringExtra(MainActivity.EXTRA_MESSAGE);
// Create the text view
TextView textView = (TextView)findViewById(R.id.textView1);
textView.setTextSize(40);
textView.setText("Welcome" + message + "!!!" );
Button sq = (Button)findViewById(R.id.sq);
Button exit = (Button)findViewById(R.id.exit);
sq.setOnClickListener(new OnClickListener()
{
@Override
public void onClick(View V)
{
Intent main = new Intent(Welwithname.this,MainScreen.class);
startActivity(main);
}
});
// Set the text view as the activity layout
setContentView(textView);
}
...
}
我应该在此代码中进行哪些更改以使其运行?