【发布时间】:2017-06-29 12:47:26
【问题描述】:
首先,我知道之前有人问过很多类似的问题,但没有一个回答我的问题。
我会保持简单。
我有一个名为 MainActivity 的类,其中包含以下代码:
public class MainActivity extends AppCompatActivity {
Car myCar = new Car();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
getSupportFragmentManager().beginTransaction().add(R.id.buttonsContainer,myCar,"me").commitNow();
View v = myCar.getView(); // returns null
Button b = v.findViewById(R.id.submitButton); //throws an exception as I'm trying to execute the "findViewById" method on a null object , as the debugger say .
b.setText("It works !"); // it doesn't
}
}
我有一个 fragment,其中包含一个带有以下代码的按钮:
public class Car extends Fragment {
View view;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.car, container, false);
return rootView;
}
}
它只显示与其对应的布局。
这是我的 MainActivity XML 文件:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:id="@+id/buttonsContainer"
tools:context="com.example.myapplication.MainActivity">
</LinearLayout>
这是我的fragment XML 代码:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="wrap_content">
<Button
android:id="@+id/submitButton"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Button" />
</LinearLayout>
我想要做的基本上是我想在单击其他内容时更改按钮文本(它必须发生在父级 activity 上。
我已经知道如何在片段活动中做到这一点)。
我还想从MainActivity 而不是fragment activity 本身为该按钮设置一个onClickListner,但是每当我尝试从主activity 访问该fragment 时,我都会得到@987654338 @ 值(无论我是想获取其Activity 或View 的引用,如myCar.getActivity() 或MyCar.getView())。
提前致谢。
Logcat:
AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.myapplication, PID: 27060
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.myapplication/com.example.myapplication.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.View android.view.View.findViewById(int)' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2665)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2726)
at android.app.ActivityThread.-wrap12(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1477)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6119)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.View android.view.View.findViewById(int)' on a null object reference
at com.example.myapplication.MainActivity.onCreate(MainActivity.java:23)
at android.app.Activity.performCreate(Activity.java:6679)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1118)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2618)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2726)
at android.app.ActivityThread.-wrap12(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1477)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6119)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)
【问题讨论】:
-
试试
public class Car extends MainActivity。 -
你能发布日志跟踪吗?
-
我编辑了我的问题以包含日志跟踪,@DJhon
-
@Abhi 我会试试看会发生什么
-
@Abhi。对它进行 Activity 没有任何意义。您将如何使用 FragmentManager 在 Activity 中显示 Activity?