Android 找不到id为的视图。。。对于片段占位符片段
我有一个简单的应用程序(基于);它唯一能做的就是显示一个编辑文本和一个按钮。该按钮创建另一个活动,并显示编辑文本消息。。。简单。。。但在emulator上运行时,当我单击按钮时,应用程序关闭,出现以下错误: “未找到片段占位符片段{4173ead8#0 id=0x7f05003c}的id 0x7f05003c(com.example.testapp:id/container)的视图” MainActivity.javaAndroid 找不到id为的视图。。。对于片段占位符片段,android,android-fragments,Android,Android Fragments,我有一个简单的应用程序(基于);它唯一能做的就是显示一个编辑文本和一个按钮。该按钮创建另一个活动,并显示编辑文本消息。。。简单。。。但在emulator上运行时,当我单击按钮时,应用程序关闭,出现以下错误: “未找到片段占位符片段{4173ead8#0 id=0x7f05003c}的id 0x7f05003c(com.example.testapp:id/container)的视图” MainActivity.java public class MainActivity extends Acti
public class MainActivity extends ActionBarActivity {
public final static String EXTRA_MESSAGE = "com.example.testapp.MESSAGE";
public void SendMessage (View view) {
Intent intent = new Intent(this, DisplayMessageActivity.class);
EditText editText = (EditText) findViewById(R.id.edt_message);
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())
.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.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;
}
}
}
public class DisplayMessageActivity extends ActionBarActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_display_message);
if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction()
.add(R.id.container, new PlaceholderFragment()).commit();
}
Intent intent = getIntent();
String message = intent.getStringExtra(MainActivity.EXTRA_MESSAGE);
TextView textView = new TextView(this);
textView.setTextSize (40);
textView.setText (message);
setContentView (textView);
}
@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;
}
}
}
活动_layout.xml
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.testapp.MainActivity"
tools:ignore="MergeRootFrame" />
<LinearLayout 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:orientation="horizontal" >
<EditText
android:id="@+id/edt_message"
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:hint="@string/Message" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/textButtom"
android:onClick="SendMessage" />
</LinearLayout>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.testapp.DisplayMessageActivity"
tools:ignore="MergeRootFrame" />
<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" >
</RelativeLayout>
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.testapp"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="10"
android:targetSdkVersion="18" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.example.testapp.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.testapp.DisplayMessageActivity"
android:label="@string/title_activity_display_message"
android:parentActivityName="com.example.testapp.MainActivity" >
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="com.example.testapp.MainActivity" />
</activity>
</application>
</manifest>
活动显示消息.xml
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.testapp.MainActivity"
tools:ignore="MergeRootFrame" />
<LinearLayout 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:orientation="horizontal" >
<EditText
android:id="@+id/edt_message"
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:hint="@string/Message" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/textButtom"
android:onClick="SendMessage" />
</LinearLayout>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.testapp.DisplayMessageActivity"
tools:ignore="MergeRootFrame" />
<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" >
</RelativeLayout>
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.testapp"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="10"
android:targetSdkVersion="18" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.example.testapp.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.testapp.DisplayMessageActivity"
android:label="@string/title_activity_display_message"
android:parentActivityName="com.example.testapp.MainActivity" >
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="com.example.testapp.MainActivity" />
</activity>
</application>
</manifest>
崩溃是由于
setContentView
(textView);在显示消息活动中
。如果您试图仅显示消息
您通过了意图,那么您可以直接将文本视图
设置为“内容到细节”活动。将DetailMessageActivity
onCreate
更改如下代码
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Intent intent = getIntent();
String message = intent.getStringExtra(MainActivity.EXTRA_MESSAGE);
TextView textView = new TextView(this);
textView.setTextSize (40);
textView.setText (message);
setContentView (textView);
}
还有一个问题,更改“MainActivity”setContentView(R.layout.activity\u布局) 有效的方法是删除以下内容:
if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction()
.add(R.id.container, new PlaceholderFragment()).commit();
}
(从DisplayMessageActivity的onCreate方法中删除它)
删除之前的if语句对我也有效 请发布您的活动布局xmls和logcat。否则就很难找出问题所在。错误很可能出现在活动的布局中。也请发布这些,而不仅仅是片段布局。您的问题中仍然缺少布局活动\u display\u message.xml。所有布局都发布在我可以找到其他布局的地方,如果它们不都是,因为这是我工作的唯一文件…您的DisplayMessageActivity中的布局。这个:R.layout.activity\u display\u消息。你可以在res->layout中找到all。占位符片段中的sendmages不起作用,编译器要求我创建一个新类和其他东西,但是你确定这是否起作用?可以给我举个例子吗?为按钮添加一个单击侦听器并调用sendMessageJajaja。。。如果(savedInstanceState==null){getSupportFragmentManager().beginTransaction().add(R.id.container,new Placeholder Fragment()).commit();}是默认情况下Eclipse生成的行。问题只是没有定义R.id.container。教程代码是错误的。因此,删除该部分是可行的。