首先,我知道之前有很多类似的问题,但没有一个能回答我的问题。
我会保持简单。
我有一个名为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
上发生。
我已经知道如何在片段活动中做到这一点。
我还希望从onClickListner
而不是MainActivity
fragment
本身设置activity
到该按钮,但每当我尝试访问fragment
时从主activity
我获得null
值(无论我是否尝试获取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)
答案 0 :(得分:0)
显然我误解了你的问题。要从父活动访问片段,过程类似,这是您可以实现的方式:
MyFragment myFragment;
myFragment = (MyFragment)getSupportFragmentManager().findFragmentByTag("MyFragmentTag");
if(myFragment != null && myFragment.isAdded()) // you need to verify if the instance is actually added to the activity
myFragment.submitButton.setText("It works");
答案 1 :(得分:0)
您的问题是,当您在代码中创建片段并添加片段时,片段的视图将不会膨胀,因此它不存在。如果添加Log.v(“Trace”,“End of onCreate”);在onCreate和Log.v结尾(“跟踪”,“汽车视图膨胀”);在CarC片段的onCreateView中,您将看到这一点。您应该做的是更改它,以便在片段视图的膨胀内更改按钮文本。 @Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View rootView = inflater.inflate(R.layout.fragment_car, container, false);
Button b = (Button)rootView.findViewById(R.id.submitButton);
b.setText("It works !");
return rootView;
}
答案 2 :(得分:0)
尝试下面的Car Fragment代码
public class Car extends Fragment {
View view;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
view = inflater.inflate(R.layout.car, container, false);
return view;
}
@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
Button b = (Button) view.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
}
}
和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").commit();
}
}