我正在开发一个测验应用。在那里,我正在给我的地图充气20次并在一次活动中移除。代码运行良好,但问题是它在某些设备上崩溃,并非总是如此。
我在加载新问题时调用此方法
public void nextQuestion(View v)
{
mCountTimer.cancel();
if(mQuestion.get(mQuestionCounter-1).getType() == Type.Pin)
{
android.support.v4.app.Fragment fragment = (getSupportFragmentManager().findFragmentById(R.id.mapFragment));
if(fragment != null)
{
FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
ft.remove(getSupportFragmentManager().findFragmentById(R.id.mapFragment)).commit();
getSupportFragmentManager().popBackStackImmediate();
mMain.removeAllViews();
}
}
loadQuestion();
}
我在加载新问题之前删除了片段,并且我的代码是
public void loadQuestion()
{
if(mQuestionCounter > 0)
{
mView = getLayoutInflater().inflate(R.layout.activity_map, mMain,false);
mMain.removeAllViews();
mMain.addView(mView);
mDisplayQuestion = (TextViewPlus)findViewById(R.id.question);
mDisplayQuestion.setText(genQuestion.getQuestion());
new Handler().post(new Runnable() {
@Override
public void run() {
getMapView((SupportMapFragment)getSupportFragmentManager().findFragmentById(R.id.mapFragment));
}
});
}
getSupportActionBar().setTitle("Question " + mQuestionCounter + " of " + QUESTION_COUNT);
}
这是我的XAML
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:foo="http://schemas.android.com/apk/res/org.buildmlearn.learnfrommap"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@android:color/black" >
<fragment
android:id="@+id/mapFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
class="com.google.android.gms.maps.SupportMapFragment" />
...
</RelativeLayout>
我在应用程序崩溃时的LogCat是
07-16 18:19:28.362: E/AndroidRuntime(22808): FATAL EXCEPTION: main
07-16 18:19:28.362: E/AndroidRuntime(22808): Process: org.example.learnfrommap, PID: 22808
07-16 18:19:28.362: E/AndroidRuntime(22808): android.view.InflateException: Binary XML file line #7: Error inflating class fragment
07-16 18:19:28.362: E/AndroidRuntime(22808): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:713)
07-16 18:19:28.362: E/AndroidRuntime(22808): at android.view.LayoutInflater.rInflate(LayoutInflater.java:755)
07-16 18:19:28.362: E/AndroidRuntime(22808): at android.view.LayoutInflater.inflate(LayoutInflater.java:492)
07-16 18:19:28.362: E/AndroidRuntime(22808): at android.view.LayoutInflater.inflate(LayoutInflater.java:397)
07-16 18:19:28.362: E/AndroidRuntime(22808): at org.buildmlearn.learnfrommap.GameActivity.loadQuestion(GameActivity.java:302)
07-16 18:19:28.362: E/AndroidRuntime(22808): at org.buildmlearn.learnfrommap.GameActivity.nextQuestion(GameActivity.java:140)
07-16 18:19:28.362: E/AndroidRuntime(22808): at org.buildmlearn.learnfrommap.GameActivity$2.onFinish(GameActivity.java:332)
07-16 18:19:28.362: E/AndroidRuntime(22808): at android.os.CountDownTimer$1.handleMessage(CountDownTimer.java:118)
07-16 18:19:28.362: E/AndroidRuntime(22808): at android.os.Handler.dispatchMessage(Handler.java:102)
07-16 18:19:28.362: E/AndroidRuntime(22808): at android.os.Looper.loop(Looper.java:136)
07-16 18:19:28.362: E/AndroidRuntime(22808): at android.app.ActivityThread.main(ActivityThread.java:5017)
07-16 18:19:28.362: E/AndroidRuntime(22808): at java.lang.reflect.Method.invokeNative(Native Method)
07-16 18:19:28.362: E/AndroidRuntime(22808): at java.lang.reflect.Method.invoke(Method.java:515)
07-16 18:19:28.362: E/AndroidRuntime(22808): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
07-16 18:19:28.362: E/AndroidRuntime(22808): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
07-16 18:19:28.362: E/AndroidRuntime(22808): at dalvik.system.NativeStart.main(Native Method)
07-16 18:19:28.362: E/AndroidRuntime(22808): Caused by: java.lang.IllegalStateException: Fragment com.google.android.gms.maps.SupportMapFragment did not create a view.
07-16 18:19:28.362: E/AndroidRuntime(22808): at android.support.v4.app.FragmentActivity.onCreateView(FragmentActivity.java:314)
07-16 18:19:28.362: E/AndroidRuntime(22808): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:685)
07-16 18:19:28.362: E/AndroidRuntime(22808): ... 15 more
我认为问题在于夸大代码。花了几个小时来调试这个,但不知道如何解决这个问题,或者如果无法修复,我怎么能抓住错误并显示一些错误信息。
修改
Logcat中的第302行是
new Handler().post(new Runnable() {
@Override
public void run() {
getMapView((SupportMapFragment)getSupportFragmentManager().findFragmentById(R.id.mapFragment));
}
});