从活动中设置片段中的TextView

时间:2014-10-19 12:46:23

标签: android android-fragments nullpointerexception

我一直在尝试在片段布局中的textView中设置文本,该片段布局来自片段实例化的活动代码。 我得到一个NullPointerException。已经好几天了,无法修复它。

我尝试过将数据从活动传递到片段的几种方法...... 1.)我首先在活动中添加了片段。然后尝试在活动布局中使用FindViewByID()访问相应的textView(最初在片段布局中)(假设它现在是我活动的一部分)。我得到了NPE。 2.)我在Fragment类中创建了setter方法。然后我试图从活动类代码中调用此方法。这也给了NPE。

第2步代码如下。请帮助我,我做错了什么。

活动代码

public class DetailsActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_details);
        // Show the Up button in the action bar.
        setupActionBar();



        //attach fragment
        Details_fragment detailsFrag = new Details_fragment(); //initializes any fragments that may be used
        //data is coming from the previous activity
        Intent intent = getIntent();
        NewsData temp = new NewsData();
        temp.setHeadline(intent.getStringExtra("headline"));
        temp.setDetails(intent.getStringExtra("details"));
        temp.setImage_source(intent.getIntExtra("img_source", 1));

        detailsFrag.setDisplay_data(temp); //ERRONEOUS TODO

        FragmentManager fManager = getFragmentManager(); //prepare fragment manager
        FragmentTransaction fTransaction = fManager.beginTransaction();

        // Add fragments using fTransaction and then commit
        fTransaction.add(R.id.details_activity_container, detailsFrag, "frag_tag");
        fTransaction.commit();

        //get data from intent
        //set data in fragment views by using the setter of fragment



    }

}

**错误在这一行:** detailsFrag.setDisplay_data(temp); //ERRONEOUS TODO 那是我发表评论时,我的应用程序不会崩溃。 注意:我甚至尝试在提交片段事务后放置这些数据设置代码行。相同错误NPE

片段代码

public class Details_fragment extends Fragment {

    NewsData display_data;


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

        display_data = new NewsData();
        View view =  inflater.inflate(R.layout.details, container, false);

        return view;

    }



    public NewsData getDisplay_data() {
        return display_data;
    }


public void setDisplay_data(NewsData display_data) {
        this.display_data = display_data;

        TextView headline = (TextView) getView().findViewById(R.id.details_headline_view);
        headline.setText(display_data.getHeadline());
        TextView detail = (TextView) getView().findViewById(R.id.details_details_view);
        detail.setText(display_data.getDetails());
        ImageView image_thumb = (ImageView) getView().findViewById(R.id.details_img_view);
        image_thumb.setImageResource(display_data.getImage_source());
    }







    public void onAttach(Activity activity) {

        super.onAttach(activity);


    }

}

错误日志

10-19 17:43:10.287: E/AndroidRuntime(9798): FATAL EXCEPTION: main
10-19 17:43:10.287: E/AndroidRuntime(9798): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.wasae.newsapp/com.wasae.newsapp.DetailsActivity}: java.lang.NullPointerException
10-19 17:43:10.287: E/AndroidRuntime(9798):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2266)
10-19 17:43:10.287: E/AndroidRuntime(9798):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2316)
10-19 17:43:10.287: E/AndroidRuntime(9798):     at android.app.ActivityThread.access$600(ActivityThread.java:150)
10-19 17:43:10.287: E/AndroidRuntime(9798):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1298)
10-19 17:43:10.287: E/AndroidRuntime(9798):     at android.os.Handler.dispatchMessage(Handler.java:99)
10-19 17:43:10.287: E/AndroidRuntime(9798):     at android.os.Looper.loop(Looper.java:213)
10-19 17:43:10.287: E/AndroidRuntime(9798):     at android.app.ActivityThread.main(ActivityThread.java:5225)
10-19 17:43:10.287: E/AndroidRuntime(9798):     at java.lang.reflect.Method.invokeNative(Native Method)
10-19 17:43:10.287: E/AndroidRuntime(9798):     at java.lang.reflect.Method.invoke(Method.java:525)
10-19 17:43:10.287: E/AndroidRuntime(9798):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:741)
10-19 17:43:10.287: E/AndroidRuntime(9798):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:557)
10-19 17:43:10.287: E/AndroidRuntime(9798):     at de.robv.android.xposed.XposedBridge.main(XposedBridge.java:132)
10-19 17:43:10.287: E/AndroidRuntime(9798):     at dalvik.system.NativeStart.main(Native Method)
10-19 17:43:10.287: E/AndroidRuntime(9798): Caused by: java.lang.NullPointerException
10-19 17:43:10.287: E/AndroidRuntime(9798):     at com.wasae.newsapp.Details_fragment.setDisplay_data(Details_fragment.java:37)
10-19 17:43:10.287: E/AndroidRuntime(9798):     at com.wasae.newsapp.DetailsActivity.onCreate(DetailsActivity.java:35)
10-19 17:43:10.287: E/AndroidRuntime(9798):     at android.app.Activity.performCreate(Activity.java:5133)
10-19 17:43:10.287: E/AndroidRuntime(9798):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
10-19 17:43:10.287: E/AndroidRuntime(9798):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2230)
10-19 17:43:10.287: E/AndroidRuntime(9798):     ... 12 more

更新

遵循setArguments()方法后,我仍然得到NPE

活动代码

public class DetailsActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_details);
        // Show the Up button in the action bar.
        setupActionBar();



        //attach fragment
        Details_fragment detailsFrag = new Details_fragment(); //initializes any fragments that may be used

        Intent intent = getIntent();
        //SETTER METHOD - DOESNT WORK
//      NewsData temp = new NewsData();
//      temp.setHeadline(intent.getStringExtra("headline"));
//      temp.setDetails(intent.getStringExtra("details"));
//      temp.setImage_source(intent.getIntExtra("img_source", 1));
//      detailsFrag.setDisplay_data(temp); //ERRONEOUS TODO

        Bundle dataBundle = new Bundle();
        dataBundle.putCharSequence("headline", intent.getStringExtra("headline"));
        dataBundle.putCharSequence("details", intent.getStringExtra("details"));
        dataBundle.putInt("img_source", intent.getIntExtra("img_source", 1));

        detailsFrag.setArguments(dataBundle);

        FragmentManager fManager = getFragmentManager(); //prepare fragment manager
        FragmentTransaction fTransaction = fManager.beginTransaction();

        // Add fragments using fTransaction and then commit
        fTransaction.add(R.id.details_activity_container, detailsFrag, "frag_tag");
        //detailsFrag.setData();
        fTransaction.commit();

        //detailsFrag.setData();



    }
}

片段代码

public class Details_fragment extends Fragment {

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

        View view =  inflater.inflate(R.layout.details, container, false);

        //get arguments 
        Bundle dataBundle = new Bundle();
        dataBundle = this.getArguments();

        TextView headline = (TextView) getView().findViewById(R.id.details_headline_view);
        headline.setText(dataBundle.getString("headline"));
        TextView detail = (TextView) getView().findViewById(R.id.details_details_view);
        detail.setText(dataBundle.getString("details"));
        ImageView image_thumb = (ImageView) getView().findViewById(R.id.details_img_view);
        image_thumb.setImageResource(dataBundle.getInt("img_source"));

        return view;

    }

//  public void setData() {
//      Bundle dataBundle = new Bundle();
//      dataBundle = this.getArguments();
//      
//      TextView headline = (TextView) getView().findViewById(R.id.details_headline_view);
//      headline.setText(dataBundle.getString("headline"));
//      TextView detail = (TextView) getView().findViewById(R.id.details_details_view);
//      detail.setText(dataBundle.getString("details"));
//      ImageView image_thumb = (ImageView) getView().findViewById(R.id.details_img_view);
//      image_thumb.setImageResource(dataBundle.getInt("img_source"));
//  }

    public void onAttach(Activity activity) {

        super.onAttach(activity);


    }
}

错误记录

10-19 23:18:20.271: E/AndroidRuntime(29737): FATAL EXCEPTION: main
10-19 23:18:20.271: E/AndroidRuntime(29737): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.wasae.newsapp/com.wasae.newsapp.DetailsActivity}: java.lang.NullPointerException
10-19 23:18:20.271: E/AndroidRuntime(29737):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2266)
10-19 23:18:20.271: E/AndroidRuntime(29737):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2316)
10-19 23:18:20.271: E/AndroidRuntime(29737):    at android.app.ActivityThread.access$600(ActivityThread.java:150)
10-19 23:18:20.271: E/AndroidRuntime(29737):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1298)
10-19 23:18:20.271: E/AndroidRuntime(29737):    at android.os.Handler.dispatchMessage(Handler.java:99)
10-19 23:18:20.271: E/AndroidRuntime(29737):    at android.os.Looper.loop(Looper.java:213)
10-19 23:18:20.271: E/AndroidRuntime(29737):    at android.app.ActivityThread.main(ActivityThread.java:5225)
10-19 23:18:20.271: E/AndroidRuntime(29737):    at java.lang.reflect.Method.invokeNative(Native Method)
10-19 23:18:20.271: E/AndroidRuntime(29737):    at java.lang.reflect.Method.invoke(Method.java:525)
10-19 23:18:20.271: E/AndroidRuntime(29737):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:741)
10-19 23:18:20.271: E/AndroidRuntime(29737):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:557)
10-19 23:18:20.271: E/AndroidRuntime(29737):    at de.robv.android.xposed.XposedBridge.main(XposedBridge.java:132)
10-19 23:18:20.271: E/AndroidRuntime(29737):    at dalvik.system.NativeStart.main(Native Method)
10-19 23:18:20.271: E/AndroidRuntime(29737): Caused by: java.lang.NullPointerException
10-19 23:18:20.271: E/AndroidRuntime(29737):    at com.wasae.newsapp.Details_fragment.onCreateView(Details_fragment.java:23)
10-19 23:18:20.271: E/AndroidRuntime(29737):    at android.app.Fragment.performCreateView(Fragment.java:1699)
10-19 23:18:20.271: E/AndroidRuntime(29737):    at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:889)
10-19 23:18:20.271: E/AndroidRuntime(29737):    at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1061)
10-19 23:18:20.271: E/AndroidRuntime(29737):    at android.app.BackStackRecord.run(BackStackRecord.java:682)
10-19 23:18:20.271: E/AndroidRuntime(29737):    at android.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1443)
10-19 23:18:20.271: E/AndroidRuntime(29737):    at android.app.Activity.performStart(Activity.java:5142)
10-19 23:18:20.271: E/AndroidRuntime(29737):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2239)
10-19 23:18:20.271: E/AndroidRuntime(29737):    ... 12 more

1 个答案:

答案 0 :(得分:3)

问题是,当您调用detailsFrag.setDisplay_data(temp); //ERRONEOUS TODO onCreateView时尚未调用,getView将返回null。您应该使用setArguments,提供要在片段中显示的数据,并检索内部为getArguments的数据,例如onCreateView