0

我開發一個Android應用程序在那裏我得到這個代碼從一個例子來充當列表。此頁面是3頁ViewPager的片段活動部分。代碼:NullPointerException異常與碎片和自定義列表

import android.os.Bundle; 
import android.support.v4.app.Fragment; 
import android.view.LayoutInflater; 
import android.view.Menu; 
import android.view.View; 
import android.view.ViewGroup; 
import android.widget.TextView; 

public class OverviewFragment extends Fragment { 

private ViewGroup mContainerView; 

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

    View rootView = inflater.inflate(R.layout.fragment_overview, container, false); 

    // Initialize 
    mContainerView = (ViewGroup)getActivity().findViewById(R.id.container); 

    // Add some items 
    addItem(); 
    addItem(); 
    addItem(); 
    addItem(); 

    return rootView; 
} 

public boolean onCreateOptionsMenu(Menu menu) { 
    super.onCreateOptionsMenu(menu, null); 
    getActivity().getMenuInflater().inflate(R.menu.activity_overviewfragment, menu); 
    return true; 
} 

private void addItem() { 
    // Instantiate a new "row" view. 
    final ViewGroup newView = (ViewGroup) LayoutInflater.from(getActivity()).inflate(
      R.layout.upcoming_list_item, mContainerView, false); 

    // Set the text in the new row to a random country. 
    ((TextView) newView.findViewById(android.R.id.text1)).setText(
      UPCOMING[(int) (Math.random() * UPCOMING.length)]); 

    // Set a click listener for the "X" button in the row that will remove the row. 

    /** 
    newView.findViewById(R.id.delete_button).setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View view) { 
      // Remove the row from its parent (the container view). 
      // Because mContainerView has android:animateLayoutChanges set to true, 
      // this removal is automatically animated. 
      mContainerView.removeView(newView); 

      // If there are no rows remaining, show the empty view. 
      if (mContainerView.getChildCount() == 0) { 
       findViewById(android.R.id.empty).setVisibility(View.VISIBLE); 
      } 
     } 
    }); 
    */ 

    // Because mContainerView has android:animateLayoutChanges set to true, 
    // adding this view is automatically animated. 
    mContainerView.addView(newView, 0); 
} 

private static final String[] UPCOMING = new String[] { 
    "Apartmen loan -- 200 $", "Car fix invoice -- 300 $", "Internet bill -- 70$", "dinner -- 20$" 
}; 
} 

錯誤:

06-23 13:57:44.440: E/AndroidRuntime(6200): FATAL EXCEPTION: main 
06-23 13:57:44.440: E/AndroidRuntime(6200): Process: com.acceleratedcode.apps.myApp, PID: 6200 
06-23 13:57:44.440: E/AndroidRuntime(6200): java.lang.NullPointerException 
06-23 13:57:44.440: E/AndroidRuntime(6200):  at com.acceleratedcode.apps.myApp.OverviewFragment.addItem(OverviewFragment.java:47) 
06-23 13:57:44.440: E/AndroidRuntime(6200):  at com.acceleratedcode.apps.myApp.OverviewFragment.onCreateView(OverviewFragment.java:27) 
06-23 13:57:44.440: E/AndroidRuntime(6200):  at android.support.v4.app.Fragment.performCreateView(Fragment.java:1500) 
06-23 13:57:44.440: E/AndroidRuntime(6200):  at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:938) 
06-23 13:57:44.440: E/AndroidRuntime(6200):  at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1115) 
06-23 13:57:44.440: E/AndroidRuntime(6200):  at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:682) 
06-23 13:57:44.440: E/AndroidRuntime(6200):  at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1478) 

錯誤在於AddItem方法(很肯定)

XML中的某處:

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:background="#FFFFFF" 
android:orientation="vertical" > 

<ScrollView 
    android:layout_width="match_parent" 
    android:layout_height="220dp" 
    android:layout_alignParentBottom="true" 
    android:layout_alignParentLeft="true" > 

    <LinearLayout android:id="@+id/container" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:orientation="vertical" > 

    </LinearLayout> 

</ScrollView> 

</RelativeLayout> 
+0

到底在哪您OverviewFragment線47? – tknell

+0

我怎樣才能找到沒有手動計數?我有點新的日食:) – user3680475

+0

http://eclipse.dzone.com/articles/line-numbers-eclipse – tknell

回答

1

您的問題是最有可能在這一行中:

((TextView) newView.findViewById(android.R.id.text1)).setText(
      UPCOMING[(int) (Math.random() * UPCOMING.length)]); 

將其更改爲:(假設你的佈局TextView的ID爲「text1」中)

((TextView) newView.findViewById(R.id.text1)).setText(
      UPCOMING[(int) (Math.random() * UPCOMING.length)]); 

你引用一個已經由Android佔領,(可能)無法在找到一個id你的佈局文件。

除此之外:

嘗試使用查看,而不是ViewGroup爲貴 「的NewView的」。並使用LinearLayout而不是ViewGroup作爲「mContainerView」。

+0

現在,我得到27行錯誤:)的addItem(和線71:mContainerView.addView(NewView的,0); @Philipp Jahoda – user3680475

+0

還好吧,看我更新的答案。 –

+0

仍然在這條線拋出一個NullPoinerException:!mContainerView =(LinearLayout中)getView()findViewById(R.id.container); – user3680475

0

android.R.id.text1是不能在下面的代碼直接使用。

((TextView) newView.findViewById(android.R.id.text1)).setText( UPCOMING[(int) (Math.random() * UPCOMING.length)]);

ü應該創建一個TextView,然後使用它。

相關問題