在android中替换片段时NullPointer异常

时间:2015-06-24 16:23:15

标签: android android-fragments nullpointerexception fragmenttransaction

我想从家庭片段中呼叫ShowProductDetail Fragment但是当我致电FragmentTransaction时,我会收到NullPointerException。 这是错误日志:

06-24 21:35:21.484: E/AndroidRuntime(11633): FATAL EXCEPTION: main
06-24 21:35:21.484: E/AndroidRuntime(11633): Process: org.nick.yarntrading, PID: 11633
06-24 21:35:21.484: E/AndroidRuntime(11633): java.lang.NullPointerException
06-24 21:35:21.484: E/AndroidRuntime(11633):    at org.nick.yarntrading.Home.callproductDetails(Home.java:63)
06-24 21:35:21.484: E/AndroidRuntime(11633):    at org.nick.card_view.DataListAdapter$1.onClick(DataListAdapter.java:90)
06-24 21:35:21.484: E/AndroidRuntime(11633):    at android.view.View.performClick(View.java:4463)
06-24 21:35:21.484: E/AndroidRuntime(11633):    at android.view.View$PerformClick.run(View.java:18770)
06-24 21:35:21.484: E/AndroidRuntime(11633):    at android.os.Handler.handleCallback(Handler.java:808)
06-24 21:35:21.484: E/AndroidRuntime(11633):    at android.os.Handler.dispatchMessage(Handler.java:103)
06-24 21:35:21.484: E/AndroidRuntime(11633):    at android.os.Looper.loop(Looper.java:193)
06-24 21:35:21.484: E/AndroidRuntime(11633):    at android.app.ActivityThread.main(ActivityThread.java:5292)
06-24 21:35:21.484: E/AndroidRuntime(11633):    at java.lang.reflect.Method.invokeNative(Native Method)
06-24 21:35:21.484: E/AndroidRuntime(11633):    at java.lang.reflect.Method.invoke(Method.java:515)
06-24 21:35:21.484: E/AndroidRuntime(11633):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:824)
06-24 21:35:21.484: E/AndroidRuntime(11633):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:640)
06-24 21:35:21.484: E/AndroidRuntime(11633):    at dalvik.system.NativeStart.main(Native Method)

这是Home课程,我从中获得错误

import android.annotation.SuppressLint;
import android.app.Activity;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentTransaction;
 import android.support.v7.widget.PopupMenu.OnMenuItemClickListener;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;
import android.widget.RelativeLayout;
import org.nick.card_view.DataListAdapter;


public class Home extends Fragment implements OnMenuItemClickListener{
App a;
ListView card_list;
DataListAdapter adapter;
RelativeLayout prodctView;
Activity act;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    a =(App)getActivity().getApplication();
    act = getActivity();
}
@SuppressLint("InflateParams") @Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

     prodctView = (RelativeLayout)inflater.inflate(R.layout.home_listview,null);
        card_list = (ListView)prodctView.findViewById(android.R.id.list);
    adapter = new DataListAdapter(a,getActivity().getSupportFragmentManager());
    card_list.setAdapter(adapter);

    return prodctView;
}
@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
    // TODO Auto-generated method stub
    super.onCreateOptionsMenu(menu, inflater);
    inflater.inflate(R.menu.main, menu);
}
@Override
public boolean onMenuItemClick(MenuItem arg0) {
    // TODO Auto-generated method stub
    return false;
}
public void callproductDetails()
{

    Fragment d = new ShowProductDetail();
    FragmentTransaction ft =getActivity().getSupportFragmentManager().beginTransaction();
    ft.setCustomAnimations(R.anim.slide_right, android.R.anim.slide_out_right,
            android.R.anim.slide_in_left, android.R.anim.slide_out_right );
    ft.replace(R.id.frame, d);
    ft.commit();
}
@Override
 public void onResume() {
    // TODO Auto-generated method stub
    super.onResume();
    if(!((App)getActivity().getApplication()).fragmentStack.contains(this))
        ((App)getActivity().getApplication()).fragmentStack.add(this);
}
public void redirect()
{
  FragmentTransaction ft = ((FragmentActivity) act).getSupportFragmentManager().beginTransaction();
    ft.setCustomAnimations(R.anim.slide_right,android.R.anim.slide_out_right);
    ft.replace(R.id.frame, a.fragmentStack.lastElement()).commit();
}

 }

我在callproductDetails()方法DataListAdapter上调用view.setOnclickListener方法。 任何想法都表示赞赏。 提前谢谢......

1 个答案:

答案 0 :(得分:0)

嗯,基本上,我不建议使用

getActivity()

作为getSupportFragmentManager的上下文,因为getActivity()可以轻松返回null值并抛出NullPointerException。

尝试给出不同的上下文,或至少将其包装在

if (getActivity() != null) 

至少你知道你的背景是你的问题。