主要活动不开始
问题描述:
我在我的程序中有两个活动,一个是在文本框中输入内容,然后单击一个按钮将它发送到另一个显示它的地方。虽然,它正在显示的那个首先出现。 这可能是他们在同一时间开始,我不确定。 我的主要活动主要活动不开始
public class MainActivity extends ActionBarActivity {
public final static String EXTRA_MESSAGE = " com.mycompany.myfirstapp.MESSAGE";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
startActivity(new Intent(getApplication(), second.class));
}
public void sendMessage(View view) {
Intent intent = new Intent(this, second.class);
EditText editText = (EditText) findViewById(R.id.edit_message);
String message = editText.getText().toString();
intent.putExtra(EXTRA_MESSAGE, message);
startActivity(intent);
// Do something in response to button
}
与
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentStart="true">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/button_send"
android:id="@+id/button"
android:singleLine="false"
android:focusableInTouchMode="true"
android:onClick="sendMessage" />
<EditText
android:layout_height="wrap_content"
android:id="@+id/edit_message"
android:hint="@string/edit_message"
android:layout_weight="1"
android:layout_width="0dp" />
</LinearLayout>
</RelativeLayout>
我的Android清单
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".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=".second"
android:label="@string/title_activity_second"
android:parentActivityName=".MainActivity" >
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="com.mycompany.myfirstapp.MainActivity" />
</activity>
</application>
我的第二个java文件
去的xml文件
答
那么,如果你打算用intent来调用second.class,那么你将需要在清单的活动内部标记一个intent-filter。这与第一类相似。
<intent-filter>
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
所以您的Android清单应该是这个样子:
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".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=".second"
android:label="@string/title_activity_second"
android:parentActivityName=".MainActivity" >
<intent-filter>
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="com.mycompany.myfirstapp.MainActivity" />
</activity>
了解更多关于Intents here。
答
你行:
startActivity(new Intent(getApplication(), second.class));
只要第一个开始启动的第二个活动。
删除这行,因为你已经在你的sendMessage()中做了你所需要的。
谢谢你现在的工作 – user3555068 2015-02-08 20:35:57
通常人们会接受一个答案(左边的复选标记),如果它解决了他们的问题。这里是一个[旅游](/游览)与一些不错的信息。 – tachyonflux 2015-02-08 23:12:51