2015-09-09 52 views
-2

我想從Fragment Activity移動到另一個Fragment Activity,但是我在設備上運行時得到NullPointerException,而不是在模擬器上正確運行任何幫助?謝謝。當從FragmentActivity移動到另一個FragmentActivity時出現NullPointerException

編輯:這是logcat的

09-10 00:24:05.139: E/AndroidRuntime(20107): FATAL EXCEPTION: main 
09-10 00:24:05.139: E/AndroidRuntime(20107): java.lang.RuntimeException: Unable to start activity ComponentInfo{inet.firstproject/inet.firstproject.MainViewActivity}: java.lang.NullPointerException 
09-10 00:24:05.139: E/AndroidRuntime(20107): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2255) 
09-10 00:24:05.139: E/AndroidRuntime(20107): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2309) 
09-10 00:24:05.139: E/AndroidRuntime(20107): at android.app.ActivityThread.access$700(ActivityThread.java:157) 
09-10 00:24:05.139: E/AndroidRuntime(20107): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1289) 
09-10 00:24:05.139: E/AndroidRuntime(20107): at android.os.Handler.dispatchMessage(Handler.java:99) 
09-10 00:24:05.139: E/AndroidRuntime(20107): at android.os.Looper.loop(Looper.java:176) 
09-10 00:24:05.139: E/AndroidRuntime(20107): at android.app.ActivityThread.main(ActivityThread.java:5317) 
09-10 00:24:05.139: E/AndroidRuntime(20107): at java.lang.reflect.Method.invokeNative(Native Method) 
09-10 00:24:05.139: E/AndroidRuntime(20107): at java.lang.reflect.Method.invoke(Method.java:511) 
09-10 00:24:05.139: E/AndroidRuntime(20107): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1102) 
09-10 00:24:05.139: E/AndroidRuntime(20107): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:869) 
09-10 00:24:05.139: E/AndroidRuntime(20107): at dalvik.system.NativeStart.main(Native Method) 
09-10 00:24:05.139: E/AndroidRuntime(20107): Caused by: java.lang.NullPointerException 
09-10 00:24:05.139: E/AndroidRuntime(20107): at inet.firstproject.MainPageActivity.onCreateView(MainPageActivity.java:64) 
09-10 00:24:05.139: E/AndroidRuntime(20107): at android.support.v4.app.Fragment.performCreateView(Fragment.java:1460) 
09-10 00:24:05.139: E/AndroidRuntime(20107): at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:911) 
09-10 00:24:05.139: E/AndroidRuntime(20107): at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1088) 
09-10 00:24:05.139: E/AndroidRuntime(20107): at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1070) 
09-10 00:24:05.139: E/AndroidRuntime(20107): at android.support.v4.app.FragmentManagerImpl.dispatchActivityCreated(FragmentManager.java:1861) 
09-10 00:24:05.139: E/AndroidRuntime(20107): at android.support.v4.app.FragmentActivity.onStart(FragmentActivity.java:547) 
09-10 00:24:05.139: E/AndroidRuntime(20107): at android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1181) 
09-10 00:24:05.139: E/AndroidRuntime(20107): at android.app.Activity.performStart(Activity.java:5336) 
09-10 00:24:05.139: E/AndroidRuntime(20107): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2227) 
09-10 00:24:05.139: E/AndroidRuntime(20107): ... 11 more 

這是oncreateView

public View onCreateView(LayoutInflater inflater, ViewGroup container, 
     Bundle savedInstanceState) { 
     if (container == null) { 
     return null; 
    } 
    String []athathTypes={"ggg","lll"}; 
    Bundle bundle = this.getArguments(); 
    int type = 0; ; 
    if(bundle!=null) 
     type= (int) bundle.getLong("Type"); 


    if(isOnline()){ 
      con.delegate= this; 
      if(isOnline()){ 
       //con.execute(" http://google.com"); 
      } 
      else 
      { 
       toast("off line "); 
      } 
    } 
    LinearLayout l= (LinearLayout)inflater.inflate(R.layout.activity_main_page, container, false); 
    athathList=(ListView) l.findViewById(R.id.listView1); 
    // athathType=(TextView) l.findViewById(R.id.athathtype); 
    // athathType.setText(athathTypes[type]); 
    String []images={"a","b"};; 
    String[] details={"a","b"};; 
    String [] discountedprice={"a","b"};; 
    String [] price={"a","b"};; 
    String []discount={"a","b"};; 
    athathList.setAdapter(new MainAdapter(getActivity(),images,details,discountedprice,price,discount)); 
    ImageButton deps=(ImageButton) l.findViewById(R.id.departments); 
    deps.setOnClickListener(new OnClickListener() {//this is line #64 

     @Override 
     public void onClick(View arg0) { 
      Intent selctionAct=new Intent(getActivity(), SelectionActivity.class); 
      startActivity(selctionAct); 

     } 
    }); 
    return l; 
} 
+2

logcat。發表它。 – poss

+2

@Hosny代碼在哪裏? –

+2

@Hosny從'MainPageActivity.java'發佈你的'onCreateView'方法代碼,並且請標記它的哪一行是#64 – Jon

回答

0
ImageButton deps=(ImageButton) l.findViewById(R.id.departments); 

將返回null

當你在真實設備上運行而不是仿真器時,你會看到這個原因,我懷疑是因爲你有多個爲activity_main_page定義的佈局。檢查此文件的所有版本(橫向/縱向/大/等..),並確保它們包含圖像按鈕ID「部門」。

相關問題