gpt4 book ai didi

android - fragment 中的空指针异常

转载 作者:塔克拉玛干 更新时间:2023-11-02 22:21:44 25 4
gpt4 key购买 nike

我在从另一个 fragment 启动一个 fragment 时收到 NullPointerException。我试图在动态启动 fragment 后调用第二个 fragment 中的方法。

这是我的日志:

05-20 09:58:31.907: E/AndroidRuntime(2585): FATAL EXCEPTION: main
05-20 09:58:31.907: E/AndroidRuntime(2585): java.lang.NullPointerException
05-20 09:58:31.907: E/AndroidRuntime(2585): at com.exercise.FragmentTest.MyFragment3.setImage(MyFragment3.java:22)
05-20 09:58:31.907: E/AndroidRuntime(2585): at com.exercise.FragmentTest.FragmentTestActivity$1.onClick(FragmentTestActivity.java:48)
05-20 09:58:31.907: E/AndroidRuntime(2585): at android.view.View.performClick(View.java:4204)
05-20 09:58:31.907: E/AndroidRuntime(2585): at android.view.View$PerformClick.run(View.java:17355)
05-20 09:58:31.907: E/AndroidRuntime(2585): at android.os.Handler.handleCallback(Handler.java:725)
05-20 09:58:31.907: E/AndroidRuntime(2585): at android.os.Handler.dispatchMessage(Handler.java:92)
05-20 09:58:31.907: E/AndroidRuntime(2585): at android.os.Looper.loop(Looper.java:137)
05-20 09:58:31.907: E/AndroidRuntime(2585): at android.app.ActivityThread.main(ActivityThread.java:5041)
05-20 09:58:31.907: E/AndroidRuntime(2585): at java.lang.reflect.Method.invokeNative(Native Method)
05-20 09:58:31.907: E/AndroidRuntime(2585): at java.lang.reflect.Method.invoke(Method.java:511)
05-20 09:58:31.907: E/AndroidRuntime(2585): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
05-20 09:58:31.907: E/AndroidRuntime(2585): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
05-20 09:58:31.907: E/AndroidRuntime(2585): at dalvik.system.NativeStart.main(Native Method)

还有我的主要 Activity 代码:

public class FragmentTestActivity extends FragmentActivity{

Fragment fragment;
Button btnFragment1, btnFragment2, btnFragment3;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
btnFragment1 = (Button)findViewById(R.id.displayfragment1);
btnFragment2 = (Button)findViewById(R.id.displayfragment2);
btnFragment3 = (Button)findViewById(R.id.displayfragment3);

// get an instance of FragmentTransaction from your Activity
FragmentManager fragmentManager = getSupportFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();

//add a fragment
MyFragment myFragment = new MyFragment();
fragmentTransaction.add(R.id.myfragment, myFragment);
fragmentTransaction.commit();

btnFragment1.setOnClickListener(btnFragmentOnClickListener);
btnFragment2.setOnClickListener(btnFragmentOnClickListener);
btnFragment3.setOnClickListener(btnFragmentOnClickListener);
}

Button.OnClickListener btnFragmentOnClickListener
= new Button.OnClickListener(){

@Override
public void onClick(View v) {


if(v == btnFragment3){
MyFragment3 newfragment = new MyFragment3();

FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();

// Replace whatever is in the fragment_container view with this fragment,
// and add the transaction to the back stack
transaction.replace(R.id.myfragment, newfragment);
transaction.addToBackStack(null);

// Commit the transaction
transaction.commit();
newfragment.setImage();
return;

}


// TODO Auto-generated method stub
Fragment newFragment = null;

// Create new fragment
if(v == btnFragment1){
newFragment = new MyFragment();
}
else if(v == btnFragment2){
newFragment = new MyFragment2();
}


FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();

// Replace whatever is in the fragment_container view with this fragment,
// and add the transaction to the back stack
transaction.replace(R.id.myfragment, newFragment);
transaction.addToBackStack(null);

// Commit the transaction
transaction.commit();

// Create new transaction

}};

}

我的 Fragment3 类是:

public class MyFragment3 extends Fragment {
ImageView iv;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// TODO Auto-generated method stub
View myFragmentView = inflater.inflate(R.layout.fragmentlayout3, container, false);
iv = (ImageView)myFragmentView.findViewById(R.id.image);

return myFragmentView;
}
public void setImage(){
iv.setImageResource(R.drawable.penguins);
}
}

请帮我解决这个问题。

最佳答案

你的错误发生在这里:

FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
newfragment.setImage();

它的发生是因为变量 iv 仍然是 null

您必须等到 Fragment 显示(膨胀)后才能访问膨胀到其中的 View 。

有关更多信息,请尝试阅读 Fragment lifecycle .

onCreateView()
The system calls this when it's time for the fragment to draw its user interface for the first time. To draw a UI for your fragment, you must return a View from this method that is the root of your fragment's layout. You can return null if the fragment does not provide a UI.

创建它的一个很好的方法是使用 setArgs 方法并传递一个包含图像信息的 Bundle

尝试使用这段代码:

public MyFragment3 extends Fragment {

public static final IMAGE_RES = "IMAGE_RES";
private int imageRes;
private ImageView iv;

public static MyFragment3 init(int imageRes) {
MyFragment3 frag = new MyFragment3();

Bundle args = new Bundle();
args.putInt(IMAGE_RES, imageRes);
frag.setArguments(args);

return frag;
}

public void onCreate(Bundle savedInstanceState) {
super.onCreate(SavedInstanceState);

imageRes = (getArguments() != null)? getArguments().getInt(IMAGE_RES) : 0;
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View myFragmentView = inflater.inflate(R.layout.fragmentlayout3, container, false);
iv = (ImageView)myFragmentView.findViewById(R.id.image);
iv.setImageResource(imageRes);

return myFragmentView;
}
}

关于android - fragment 中的空指针异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16647552/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com