インテントを使用してあるアクティビティから別のアクティビティに切り替えようとしています。最初のアクティビティは正常に実行されていますが、2 番目のアクティビティが開始されるとアプリが停止します。これにコードを添付しました。
マニフェスト :
<?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);
}
});
}
}
2 番目のアクティビティ:
このアクティビティは、アクティビティ 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);
}
...
}
このコードを実行するには、このコードにどのような変更を加える必要がありますか?