我遵循了#34; Android Programming"由大书呆子牧场。当我在手机上运行应用程序时,它说"应用程序意外停止了。再试一次"。这是它在LogCat中显示的第一行:
10-21 10:11:48.550: E/FragmentManager(6393): No view found for id 0x7f05003c (com.bignerdranch.android.criminalIntent:id/fragmentContainer) for fragment CrimeFragment{4054c7d0 #0 id=0x7f05003c}
10-21 10:11:48.550: E/FragmentManager(6393): Activity state:

这是我有fragmentContainer的地方:
package com.bignerdranch.android.criminalIntent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.app.ActionBarActivity;
import android.view.Menu;
import android.view.MenuItem;
public class CrimeActivity extends ActionBarActivity {
//Call when the activity is first created
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_crime);
android.support.v4.app.FragmentManager fm = getSupportFragmentManager();
Fragment fragment = fm.findFragmentById(R.id.fragmentContainer);
if (fragment == null){
fragment = new CrimeFragment();
fm.beginTransaction()
.add(R.id.fragmentContainer, fragment)
.commit();
} //Create a new fragment transaction, include one add operation in it, and then commit it
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.crime, 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);
}
}

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:id="@+id/fragmentContainer" >
<EditText android:id="@+id/crime_title"
android:layout_width ="match_parent"
android:layout_height = 'wrap_content'
android:hint = "@string/crime_title_hint"
/>
</LinearLayout>
&#13;
以下是清单以及java和xml文件的其余部分:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.bignerdranch.android.criminalIntent"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="21" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".CrimeActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
&#13;
CrimeFragment.java
package com.bignerdranch.android.criminalIntent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
public class CrimeFragment extends Fragment {
private Crime mCrime;
private EditText mTitleField;
@Override
public void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
mCrime = new Crime();
}
@Override
public View onCreateView(LayoutInflater inflater,
@Nullable ViewGroup parent, @Nullable Bundle savedInstanceState) {
// inflates fragment_crime.xml
//onCreateView method is also the place to wire up the EditText to respond to user input
View v = inflater.inflate(R.layout.fragment_crime, parent, false);
mTitleField = (EditText)v.findViewById(R.id.crime_title);
mTitleField.addTextChangedListener(new TextWatcher(){
public void onTextChanged(
CharSequence c, int start, int before, int count){
mCrime.setTitle(c.toString()); //set title, CharSequence is user's input
}
public void beforeTextChanged(CharSequence c, int start, int count, int after){
}
public void afterTextChanged(Editable c){
}
});
return v;
}
}
&#13;
Crime.java
package com.bignerdranch.android.criminalIntent;
import java.util.UUID;
public class Crime {
private UUID mId;
private String mTitle;
public Crime() {
//Generate unique identifier
mId = UUID.randomUUID();
}
public String getTitle() {
return mTitle;
}
public void setTitle(String title) {
mTitle = title;
}
public UUID getId() {
return mId;
}
}
&#13;
分别在res / layout中的
fragment_crime.xml和activity_crime.xml:
<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"
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.bignerdranch.android.criminalIntent.CrimeActivity" >
</RelativeLayout>
&#13;
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:id="@+id/fragmentContainer" >
<EditText android:id="@+id/crime_title"
android:layout_width ="match_parent"
android:layout_height = 'wrap_content'
android:hint = "@string/crime_title_hint"
/>
</LinearLayout>
&#13;
res / menu中的
crime.xml:
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
tools:context="com.bignerdranch.android.criminalIntent.CrimeActivity" >
<item
android:id="@+id/action_settings"
android:orderInCategory="100"
app:showAsAction="never"/>
</menu>
&#13;
我是Android应用程序开发的新手,所以每个意见都非常感谢。谢谢!
答案 0 :(得分:1)
所以我会用这种方式简化 onCreate()。容器检查不需要它自己的具体对象,它的引用类型是LinearLayout而不是Fragment。此外,在您知道自己需要它之前,即在空检查之后,您也不想要求片段管理器。
public class CrimeActivity extends ActionBarActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_crime);
if (findViewById(R.id.fragmentContainer != null) {
CrimeFragment fragment = new CrimeFragment();
getSupportFragmentManager().beginTransaction()
.add(R.id.fragmentContainer, fragment).commit();
}
}
请试试。