您好我不熟悉Android的东西。现在我正在按照developer.android.com/training/basics/firstapp/starting-activity.html中给出的教程和构建我的拳头app.It很好,直到我处理单一活动。后来我添加了第二个活动。当我点击第一个按钮时,它应该被定向到第二个活动。但是点击第一个活动我的应用程序突然停止。请帮助我。 这是logcat
06-23 23:10:50.134:E / FragmentManager(437):找不到id的视图 片段的0x7f05003c(com.example.honey:id / container) PlaceholderFragment {43e98050#0 id = 0x7f05003c} 06-23 23:10:50.134: E / FragmentManager(437):活动状态:06-23 23:10:50.264: E / AndroidRuntime(437):致命异常:主06-23 23:10:50.264: E / AndroidRuntime(437):java.lang.RuntimeException:无法启动 活动 ComponentInfo {com.example.honey / com.example.honey.DisplayMessageActivity}: java.lang.IllegalArgumentException:找不到id 0x7f05003c的视图 片段的(com.example.honey:id / container) PlaceholderFragment {43e98050#0 id = 0x7f05003c} 06-23 23:10:50.264: E / AndroidRuntime(437):at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663) 06-23 23:10:50.264:E / AndroidRuntime(437):at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679) 06-23 23:10:50.264:E / AndroidRuntime(437):at android.app.ActivityThread.access $ 2300(ActivityThread.java:125)06-23 23:10:50.264:E / AndroidRuntime(437):at android.app.ActivityThread $ H.handleMessage(ActivityThread.java:2033) 06-23 23:10:50.264:E / AndroidRuntime(437):at android.os.Handler.dispatchMessage(Handler.java:99)06-23 23:10:50.264:E / AndroidRuntime(437):at android.os.Looper.loop(Looper.java:123)06-23 23:10:50.264: E / AndroidRuntime(437):at android.app.ActivityThread.main(ActivityThread.java:4627)06-23 23:10:50.264:E / AndroidRuntime(437):at java.lang.reflect.Method.invokeNative(Native Method)06-23 23:10:50.264:E / AndroidRuntime(437):at java.lang.reflect.Method.invoke(Method.java:521)06-23 23:10:50.264: E / AndroidRuntime(437):at com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:868) 06-23 23:10:50.264:E / AndroidRuntime(437):at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)06-23 23:10:50.264:E / AndroidRuntime(437):at dalvik.system.NativeStart.main(Native Method)06-23 23:10:50.264: E / AndroidRuntime(437):引起:java.lang.IllegalArgumentException: 找不到id 0x7f05003c(com.example.honey:id / container)的视图 片段PlaceholderFragment {43e98050#0 id = 0x7f05003c} 06-23 23:10:50.264:E / AndroidRuntime(437):at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:930) 06-23 23:10:50.264:E / AndroidRuntime(437):at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1115) 06-23 23:10:50.264:E / AndroidRuntime(437):at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:682) 06-23 23:10:50.264:E / AndroidRuntime(437):at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1478) 06-23 23:10:50.264:E / AndroidRuntime(437):at android.support.v4.app.FragmentActivity.onStart(FragmentActivity.java:570) 06-23 23:10:50.264:E / AndroidRuntime(437):at android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1129) 06-23 23:10:50.264:E / AndroidRuntime(437):at android.app.Activity.performStart(Activity.java:3781)06-23 23:10:50.264:E / AndroidRuntime(437):at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2636) 06-23 23:10:50.264:E / AndroidRuntime(437):... 11更多
第一个活动java文件
package com.example.honey; import android.content.Intent; import android.os.Bundle; import android.support.v4.app.Fragment; import android.support.v7.app.ActionBarActivity; import android.view.LayoutInflater; import android.view.Menu; import android.view.MenuItem; import android.view.View; import android.view.ViewGroup; import android.widget.EditText; public class MainActivity extends ActionBarActivity { static final String EXTRA_MESSAGE = "com.example.honey.MESSAGE"; public void sendMessage(View view){ Intent intent=new Intent(this,DisplayMessageActivity.class); EditText editText=(EditText)findViewById(R.id.edit_query); String message = editText.getText().toString(); intent.putExtra(EXTRA_MESSAGE, message); startActivity(intent); } @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); if (savedInstanceState == null) { getSupportFragmentManager().beginTransaction().add(R.id.container, new
PlaceholderFragment()) 。承诺(); } }
@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; } @Override public boolean onOptionsItemSelected(MenuItem item) { // Handle action bar item clicks here. The action bar will // automatically handle clicks on the Home/Up button, so long // as you specify a parent activity in AndroidManifest.xml. int id = item.getItemId(); if (id == R.id.action_settings) { return true; } return super.onOptionsItemSelected(item); } /** * A placeholder fragment containing a simple view. */ public static class PlaceholderFragment extends Fragment { public PlaceholderFragment() { } @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View rootView = inflater.inflate(R.layout.fragment_main, container, false); return rootView; } } } }
第一个活动fragment_main.xml
第二项活动
package com.example.honey;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.app.ActionBarActivity;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View; import
android.view.ViewGroup;
import android.widget.TextView;
public class DisplayMessageActivity extends ActionBarActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState); Intent intent=getIntent();
setContentView(R.layout.activity_display_message);
String message=intent.getStringExtra(MainActivity.EXTRA_MESSAGE);
TextView textView =new TextView(this);
textView.setText(message);
setContentView(textView);
if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction().add(R.id.container, new PlaceholderFragment()).commit();
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.display_message, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
/**
* A placeholder fragment containing a simple view.
*/
public static class PlaceholderFragment extends Fragment {
public PlaceholderFragment() {
}
@Override
public View onCreateView(LayoutInflater inflater,ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_display_message,container, false);
return rootView;
}
}
}
XML文件
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="com.example.honey.DisplayMessageActivity$PlaceholderFragment"
<TextView android:layout_width="wrap_content" android:layout_height="wrap_content" ></TextView> </LinearLayout>
清单
<uses-sdk android:minSdkVersion="8" android:targetSdkVersion="19" /> <application android:allowBackup="true" android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme" > <activity android:name="com.example.honey.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.honey.DisplayMessageActivity" android:label="@string/title_activity_display_message" android:parentActivityName="com.example.first.MainActivity" > <meta-data android:name="android.support.PARENT_ACTIVITY" android:value="com.example.first.MainActivity" /> </activity> </application>
答案 0 :(得分:0)
您忘了将活动添加到我认为的清单......
<activity
android:name="DisplayMessageActivity"
android:label="@string/..." >
</activity>
答案 1 :(得分:0)
检查MainActivity的XML布局,为您从未声明的EditText指定findViewById(R.id.edit_query)。
将stared行添加到fragment_main.xml文件
`<EditText
**android:id="@+id/edit_query**
android:layout_width="0dp"
android:layout_height="wrap_content"
android:hint="@string/edit_message"
android:layout_weight="1"/>`
答案 2 :(得分:0)
由于复制粘贴,您在第二个活动代码中犯了一个愚蠢的错误:
DisplayMessageActivity:
if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction().add(R.id.container, new PlaceholderFragment()).commit();
}
}
你试图将PlaceholderFragment添加到R.id.container,当第二个活动的内容视图是TextView时,它没有容器,也就是R.id.container指定的布局不存在于第二项活动中。
从第二个活动中删除这些行,它应该有效。