2016-03-05 54 views
0

以下代碼將引發NullPointerException錯誤。我無法弄清楚爲什麼。你能檢查一下嗎? enter code here在片段中獲取NullPointerException錯誤

我在一項主要活動中獲得了2個片段,如下所示。

  1. RSSFeedActivity enter code here
  2. MyListFragment(包含一個按鈕)
  3. DetailFragment(包含的TextView)

/****************** ***活動代碼*******************/

package com.example.oksijen02.unit2.multipanelFragment; 
import android.app.Activity; 
import android.os.Bundle; 
import android.util.Log; 
import com.example.oksijen02.intent.R; 

public class RSSFeedActivity extends Activity implements MyListFragment.OnItemSelectedListener{ 
    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.u2_fragment_activity_main); 
    } 
    @Override public void onRSSItemSelected(String link) {  Log.d("butona tiklandi", "onRSSItemSelected: "); 
     DetailFragment fragment = (DetailFragment) getFragmentManager().findFragmentById(R.id.detailFragment); 
     fragment.setText(link); 
    } 
} 

/**************** *** MyListFragment ************************/

package com.example.oksijen02.unit2.multipanelFragment; 

import android.app.Fragment; 
import android.content.Context; 
import android.os.Bundle; 
import android.support.annotation.Nullable; 
import android.util.Log; 
import android.view.LayoutInflater; 
import android.view.View; 
import android.view.ViewGroup; 
import android.widget.Button; 

import com.example.oksijen02.intent.R; 
public class MyListFragment extends Fragment { 
    private OnItemSelectedListener listener; 
    @Nullable 
    @Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 
     super.onCreateView(inflater, container, savedInstanceState); 
     View view = inflater.inflate(R.layout.u2_fragment_rsslist_overview,container,false); 
     Button detailButton = (Button) view.findViewById(R.id.detailButton); 

     detailButton.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 
       updateDetail("fake"); 
      } 
     }); 

     return view; 
    } 

    @Override 
    public void onAttach(Context context) { 
     super.onAttach(context); 
     Log.d("in on attach", "onAttach"); 
     if(context instanceof OnItemSelectedListener){ 
      Log.d("context is not of ", "onAttach:"); 
      listener = (OnItemSelectedListener) context; 
     }else{ 
      throw new ClassCastException(context.toString()+" must implement MyListFragment.OnItemSelectedListener"); 
     } 
    } 

    public interface OnItemSelectedListener{ 
     public void onRSSItemSelected(String link); 
    } 

    public void updateDetail(String fake) { 
     Log.d("tikladi update", "updateDetail"); 
     String newTime = String.valueOf(System.currentTimeMillis()); 
     Log.d("newtime"+newTime, "updateDetail"); 

     Log.d("listener"+listener.toString(), "updateDetail"); 
     listener.onRSSItemSelected(newTime); 
    } 

} 

/*********************** DetailFragment Code ***************** *************/

package com.example.oksijen02.unit2.multipanelFragment; 

import android.app.Fragment; 
import android.os.Bundle; 
import android.support.annotation.Nullable; 
import android.view.LayoutInflater; 
import android.view.View; 
import android.view.ViewGroup; 
import android.widget.TextView; 
import com.example.oksijen02.intent.R; 

public class DetailFragment extends Fragment { 

    @Nullable 
    @Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 
     super.onCreateView(inflater, container, savedInstanceState); 
     View view = inflater.inflate(R.layout.u2_fragment_rss_item_detail, container,false); 
     return view; 
    } 

    public void setText(String url) { 
     TextView textview = (TextView) getView().findViewById(R.id.detailsText); 
     textview.setText(url); 
    } 
} 

錯誤是如下:

PID: 15763 java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String java.lang.Object.toString()' on a null object reference at com.example.oksijen02.unit2.multipanelFragment.MyListFragment.updateDetail(MyListFragment.java:59) at com.example.oksijen02.unit2.multipanelFragment.MyListFragment$1.onClick(MyListFragment.java:30) at android.view.View.performClick(View.java:5181) at android.view.View$PerformClick.run(View.java:20887) at 

回答

0

當我搜索的網站上,我找到了答案,爲什麼onAttach法犯規呼叫。請找到該question

的答案,所以我說下面的代碼MyListFragment代碼:

@Override 
public void onAttach(Activity activity) { 
    super.onAttach(activity); 
    if(activity instanceof OnItemSelectedListener){ 
     Log.d("context is not of ", "onAttach:"); 
     listener = (OnItemSelectedListener) activity; 
    }else{ 
     throw new ClassCastException(activity.toString()+" must implement MyListFragment.OnItemSelectedListener"); 
    } 

} 
相關問題