【问题标题】:app crash on settext() fragment functionsettext() 片段函数上的应用程序崩溃
【发布时间】:2018-03-10 23:17:15
【问题描述】:

我正在学习片段之间的交流。但是,我遇到了一个问题,我不知道如何解决它。我对谷歌进行了大量研究,但没有发现任何东西,所以我希望你能帮助我。这是我的代码:

活动:

     public class NextActivity extends AppCompatActivity  {

    public static final String EXTRA_BUTTON_TAG = 

       "com.example.elieazoury.testfragments.NextActivity";

     private NextFragment nextFragment;

       @Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_next);



}

    @Override

   public void onResume() {
    super.onResume();
    // 3 - Call update method here because we are sure that DetailFragment is visible

   nextFragment=new NextFragment();
    this.updateDetailFragmentTextViewWithIntentTag();
}

private void updateDetailFragmentTextViewWithIntentTag(){   nextFragment.updateTextView();
}

} 

和片段:

    public class NextFragment extends Fragment {

  private TextView textView;

public NextFragment() {
    // Required empty public constructor
}


@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    View view = inflater.inflate(R.layout.fragment_next, container, false);

   textView = view.findViewById(R.id.texttt);

    return (view);

}

public void updateTextView( ) {


        textView.setText("literally anything");


}
 }

XML:

  <?xml version="1.0" encoding="utf-8"?>
    <fragment xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
 android:name="com.example.elieazoury.testfragments.NextFragment"
   android:layout_width="match_parent"
  android:layout_height="match_parent"
  android:layout_marginTop="260dp"
  app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
android:id="@+id/fragm"/>

 <FrameLayout 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"
 tools:context="com.example.elieazoury.testfragments.NextFragment">


<TextView
    android:layout_width="wrap_content"
    android:id="@+id/texttt"
    android:layout_height="wrap_content"
    android:text="@string/hello_blank_fragment" />

  </FrameLayout>

日志崩溃:

com.example.elieazoury.testfragments E/AndroidRuntime: FATAL EXCEPTION: main 进程:com.example.elieazoury.testfragments,PID:8727 java.lang.RuntimeException:无法恢复活动 {com.example.elieazoury.testfragments/com.example.elieazoury.testfragments.NextActivity}:java.lang.NullPointerException:尝试调用虚拟方法 'android.view.View android.support .v4.app.FragmentActivity.findViewById(int)' 在空对象引用上 在 android.app.ActivityThread.performResumeActivity(ActivityThread.java:3581) 在 android.app.ActivityThread.handleResumeActivity(ActivityThread.java:3621) 在 android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2862) 在 android.app.ActivityThread.-wrap11(未知来源:0) 在 android.app.ActivityThread$H.handleMessage(ActivityThread.java:1589) 在 android.os.Handler.dispatchMessage(Handler.java:106) 在 android.os.Looper.loop(Looper.java:164) 在 android.app.ActivityThread.main(ActivityThread.java:6494) 在 java.lang.reflect.Method.invoke(本机方法) 在 com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438) 在 com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807) 原因:java.lang.NullPointerException:尝试在空对象引用上调用虚拟方法 'android.view.View android.support.v4.app.FragmentActivity.findViewById(int)' 在 com.example.elieazoury.testfragments.NextFragment.updateTextView(NextFragment.java:38) 在 com.example.elieazoury.testfragments.NextActivity.updateDetailFragmentTextViewWithIntentTag(NextActivity.java:42) 在 com.example.elieazoury.testfragments.NextActivity.onResume(NextActivity.java:31) 在 android.app.Instrumentation.callActivityOnResume(Instrumentation.java:1355) 在 android.app.Activity.performResume(Activity.java:7117) 在 android.app.ActivityThread.performResumeActivity(ActivityThread.java:3556) 在 android.app.ActivityThread.handleResumeActivity(ActivityThread.java:3621) 在 android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2862) 在 android.app.ActivityThread.-wrap11(未知来源:0) 在 android.app.ActivityThread$H.handleMessage(ActivityThread.java:1589) 在 android.os.Handler.dispatchMessage(Handler.java:106) 在 android.os.Looper.loop(Looper.java:164) 在 android.app.ActivityThread.main(ActivityThread.java:6494) 在 java.lang.reflect.Method.invoke(本机方法) 在 com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438) 在 com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)

【问题讨论】:

  • 请学习一些android基础知识...显示nextFragment=new NextFragment();和this.updateDetailFragmentTextViewWithIntentTag();之间的onCreate被调用... 更不用说代码不适合异常...错误显然来自getActivity().findViewById(...) 在updateTextView() 内部调用
  • 我试图在我的 updateTextview() 中找到该应用程序仍然崩溃的视图
  • @Selvin - 这不是一个普通的 NPE,特别是崩溃日志显示它不是在发布者的代码中发生,而是在提供的代码中;因此,这是一个二级问题,其中声音代码已设置为失败,但存在一些较早的问题或遗漏。
  • @ChrisStratton 这是正常的 NPE ...他只是发布了他实际使用的不同代码 attempt to invoke virtual method 'android.view.View android.support.v4.app.FragmentActivity.findViewById(int)' on a null object reference at com.example.elieazoury.testfragments.NextFragment.updateTextView(NextFragment.java:38) 点显然他在第 38 行有 getActivity().findViewById(...)

标签: android android-studio


【解决方案1】:

你必须初始化变量textView。 否则,您将收到NullPointerException,正如您发布的消息所述。 将此行添加到 OnCreateView 方法中: textView = view.findViewById(R.id.your_textview_id);

您正在使用其空构造函数实例化NextFragment 的新实例,因此OnCreateView 将永远不会被命中,textView 变量将为空。 您在 DetailFragment.OnResume 中执行此操作。

nextFragment=new NextFragment();

运行时异常“无法恢复活动”正在抛出,因为通过访问新创建的实例 nextFragment 中的 null 变量会引发 NPE。

【讨论】:

  • 我已将其删除以进行测试无论是否使用查找视图,崩溃都是相同的
【解决方案2】:

在您的onResume 方法中,您正在重新实例化片段,您可以做的是通过从布局中找到您的片段来初始化您的nextFragment。

@Override
protected void onResume() {
    super.onResume();

    nextFragment = (NextFragment) getSupportFragmentManager().findFragmentById(R.id.fragm);
    updateDetailFragmentTextViewWithIntentTag();
}

private void updateDetailFragmentTextViewWithIntentTag(){   nextFragment.updateTextView();

}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-06-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多