从 Activity 调用 Fragment 时应用程序崩溃
Posted
技术标签:
【中文标题】从 Activity 调用 Fragment 时应用程序崩溃【英文标题】:App Crashing when calling a Fragment from an Activity 【发布时间】:2016-08-27 23:14:38 【问题描述】:我正在开发一个应用程序,其中有一个片段作为我的主页。我目前正在尝试从活动中调用该片段,但不幸的是它正在崩溃。我什至已经实现了 FragmentTransaction,但它仍然不想拿起它。
日志猫
05-02 22:22:40.331 5927-5927/com.example.treycoco.calorietracker E/androidRuntime: FATAL EXCEPTION: main
Process: com.example.treycoco.calorietracker, PID: 5927
java.lang.NullPointerException: Attempt to invoke virtual method' android.support.v4.app.FragmentTransactio android.support.v4.app.FragmentTransaction.replace(int, android.support.v4.app.Fragment)' on a null object reference at com.example.treycoco.calorietracker.CalorieDetails$1$1.onClick(CalorieDetails.java:79)
at android.support.v7.app.AlertController$ButtonHandler.handleMessage(AlertController.java:157)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:158)
at android.app.ActivityThread.main(ActivityThread.java:7229)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)
更新日志
example.treycoco.calorietracker E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.treycoco.calorietracker, PID: 19224
java.lang.IllegalArgumentException: No view found for id 0x7f0e0080 (com.example.treycoco.calorietracker:id/FragmentHolder) for fragment FragmentHomed567f7e #0 id=0x7f0e0080
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1059)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1252)
at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:738)
at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1617)
at android.support.v4.app.FragmentManagerImpl$1.run(FragmentManager.java:517)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:158)
at android.app.ActivityThread.main(ActivityThread.java:7229)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)
caloriedetails.java
public class CalorieDetails extends AppCompatActivity
private TextView foodName, calories, dateTaken;
private Button shareButton;
private int foodId;
private Button deleteButton;
private android.support.v4.app.FragmentManager fragmentManager;
private FragmentTransaction fragmentTransaction;
@Override
protected void onCreate(Bundle savedInstanceState)
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_calorie_details);
foodName = (TextView) findViewById(R.id.detsFoodName);
calories = (TextView) findViewById(R.id.detscaloriesValue);
dateTaken = (TextView) findViewById(R.id.detsDateText);
deleteButton = (Button) findViewById(R.id.deleteButton);
Food food = (Food) getIntent().getSerializableExtra("userObj");
foodName.setText(food.getFoodName());
calories.setText(String.valueOf(food.getCalories()));
dateTaken.setText(food.getRecordDate());
foodId = food.getFoodId();
foodName.setTextColor(Color.WHITE);
dateTaken.setTextColor(Color.WHITE);
calories.setTextSize(34.9f);
calories.setTextColor(Color.WHITE);
deleteButton.setOnClickListener(new View.OnClickListener()
@Override
public void onClick(View v)
//TODO: put delete functionality here
android.support.v7.app.AlertDialog.Builder alert = new
android.support.v7.app.AlertDialog.Builder(CalorieDetails.this);
alert.setTitle("Delete?");
alert.setMessage("Are you sure you want to delete this item?");
alert.setNegativeButton("No", null);
alert.setPositiveButton("Yes", new
DialogInterface.OnClickListener()
@Override
public void onClick(DialogInterface dialog, int which)
DatabaseHandler dba = new
DatabaseHandler(getApplicationContext());
dba.deleteFood(foodId);
Toast.makeText(CalorieDetails.this, "Food Item
Deleted!", Toast.LENGTH_SHORT).show();
FragmentHome fragmentHome = new FragmentHome()
fragmentTransaction =
getSupportedFragmentManager().beginTransaction();
fragmentTransaction.replace(R.id.FragmentHolder,
fragmentHome);
fragmentTransaction.commit();
CalorieDetails.this.finish();
);
alert.show();
);
caloriedetails.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_
android:layout_
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
tools:context=".CalorieDetails"
android:background="@drawable/imgbackground2"
style="@style/AppTheme"
>
<ImageView
android:id="@+id/logo"
android:src="@drawable/weight"
android:layout_centerHorizontal="true"
android:layout_
android:layout_ />
<LinearLayout
android:id="@+id/layout"
android:elevation="4dp"
android:orientation="vertical"
android:layout_
android:layout_
android:layout_below="@+id/logo"
android:layout_centerHorizontal="true">
<TextView
android:id="@+id/detsFoodName"
android:elevation="4dp"
android:text="dkdad"
android:textSize="19sp"
android:textStyle="bold"
android:layout_marginTop="18dp"
android:layout_gravity="center"
android:layout_
android:layout_ />
<TextView
android:id="@+id/detsCaloriesTitle"
android:text="Calories:"
android:textSize="18sp"
android:layout_marginTop="18dp"
android:layout_gravity="center_horizontal"
android:layout_
android:layout_ />
<TextView
android:id="@+id/detscaloriesValue"
android:text="200"
android:textSize="18sp"
android:layout_marginTop="18dp"
android:layout_gravity="center_horizontal"
android:textStyle="bold"
android:layout_
android:layout_ />
<TextView
android:id="@+id/detsDateText"
android:text="Consumed on..."
android:textStyle="italic"
android:textSize="14sp"
android:layout_marginTop="14dp"
android:layout_gravity="center_horizontal"
android:layout_
android:layout_ />
<Button
android:id="@+id/deleteButton"
android:text="DELETE"
android:textColor="#ffff"
android:textStyle="bold"
android:layout_marginTop="20dp"
android:layout_gravity="center_horizontal"
android:layout_
android:background="@color/colorBackground2"
android:layout_ />
</LinearLayout>
【问题讨论】:
【参考方案1】:您已经声明了 fragmentManager 和 fragmentTransaction 但尚未初始化。
FragmentHome fragmentHome = new FragmentHome();
fragmentTransaction = getSupportFragmentManager().beginTransaction();
fragmentTransaction.replace(R.id.FragmentHolder, fragmentHome);
fragmentTransaction.commit();
从 onClick 方法中删除这一行,
CalorieDetails.this.finish();
因为你要在活动中添加片段所以不要完成活动。
将此代码添加到您的 caloriedetails.xml 文件中
<LinearLayout
android:id="@+id/FragmentHolder"
android:layout_
android:layout_>
</LinearLayout>
请在开始实施前先做一些谷歌搜索。
【讨论】:
我实现了上面的代码,还是报错:/ 发布您的 activity_calorie_details.xml 文件。我想你可能没有在布局文件中添加 FrgamentHolder 我刚刚更新希望能看到问题这是我第一次使用 android studio 更新我的答案请检查 成功了 非常感谢!对不起,我认为我做对了。我需要记住在 xml 上放一个 id 再次谢谢你【参考方案2】:您的 fragmentTransaction 为 null,因为您没有对其进行初始化,因此出现异常。
这样做
FragmentTransaction fragmentTransaction = getSupportFragmentManager()
.beginTransaction();
【讨论】:
以上是关于从 Activity 调用 Fragment 时应用程序崩溃的主要内容,如果未能解决你的问题,请参考以下文章
如何从我的 Activity 中调用 Fragment 的 TextViews 和 Button?