SherlockFragment(Tab1) で試して実装したいカスタム Listview があります。
通常の拡張アクティビティではうまく機能しますが、SherlockFragmentActivity(MainActivity) で宣言しようとすると、NullPointerException が発生します。
私の MainActivity は次のようになります。
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Get the view from activity_main.xml
setContentView(R.layout.activity_main);
RecipeList = (ListView) findViewById(R.id.mainListView);
adupter = new MainListCustomBaseAdapter(ShoppingItems, this);
RecipeList.setAdapter(adupter);
//Deploying a listener menu
registerForContextMenu(RecipeList);
{
私の FragmentTab1:
public class FragmentTab1 extends SherlockFragment {
@Override
public SherlockFragmentActivity getSherlockActivity() {
return super.getSherlockActivity();
}
@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Get the view from fragmenttab1.xml
View view = inflater.inflate(R.layout.scanlist, container, false);
return view;
}
@Override
public void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
setUserVisibleHint(true);
}
}
そして私のcustomlistviewAdapter:
public class MainListCustomBaseAdapter extends BaseAdapter {
static ArrayList<ListItems> DataSomething;
static Context Cont;
public MainListCustomBaseAdapter (ArrayList<ListItems> shoppingItems, Context c){
DataSomething = shoppingItems;
Cont = c;
}
public int getCount() {
// TODO Auto-generated method stub
return DataSomething.size();
}
public Object getItem(int position) {
// TODO Auto-generated method stub
return DataSomething.get(position);
}
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
public View getView(int position, View convertView, ViewGroup parent) {
View v = convertView;
if (v == null){
LayoutInflater vi = (LayoutInflater)Cont.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = vi.inflate(R.layout.mainlistlayout, null);
}
else{
return v;
}
ImageView image = (ImageView) v.findViewById(R.id.ListImage);
TextView titleView = (TextView)v.findViewById(R.id.title);
ListItems msg = DataSomething.get(position);
image.setImageResource(msg.icon);
titleView.setText(msg.title);
return v;
}
public void updateResults(ArrayList<MainListCustomBaseAdapter> results){
notifyDataSetChanged();
}
}
エラーは次のようになります。
08-08 16:25:45.655: E/AndroidRuntime(2621): FATAL EXCEPTION: main
08-08 16:25:45.655: E/AndroidRuntime(2621): java.lang.RuntimeException: Unable to start activity ComponentInfo{org.example.com.barcoder/org.example.com.barcoder.MainActivity}: java.lang.NullPointerException
08-08 16:25:45.655: E/AndroidRuntime(2621): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2100)
08-08 16:25:45.655: E/AndroidRuntime(2621): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2125)
08-08 16:25:45.655: E/AndroidRuntime(2621): at android.app.ActivityThread.access$600(ActivityThread.java:140)
08-08 16:25:45.655: E/AndroidRuntime(2621): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1227)
08-08 16:25:45.655: E/AndroidRuntime(2621): at android.os.Handler.dispatchMessage(Handler.java:99)
08-08 16:25:45.655: E/AndroidRuntime(2621): at android.os.Looper.loop(Looper.java:137)
08-08 16:25:45.655: E/AndroidRuntime(2621): at android.app.ActivityThread.main(ActivityThread.java:4898)
08-08 16:25:45.655: E/AndroidRuntime(2621): at java.lang.reflect.Method.invokeNative(Native Method)
08-08 16:25:45.655: E/AndroidRuntime(2621): at java.lang.reflect.Method.invoke(Method.java:511)
08-08 16:25:45.655: E/AndroidRuntime(2621): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1006)
08-08 16:25:45.655: E/AndroidRuntime(2621): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:773)
08-08 16:25:45.655: E/AndroidRuntime(2621): at dalvik.system.NativeStart.main(Native Method)
08-08 16:25:45.655: E/AndroidRuntime(2621): Caused by: java.lang.NullPointerException
08-08 16:25:45.655: E/AndroidRuntime(2621): at org.example.com.barcoder.MainActivity.onCreate(MainActivity.java:74)
08-08 16:25:45.655: E/AndroidRuntime(2621): at android.app.Activity.performCreate(Activity.java:5206)
08-08 16:25:45.655: E/AndroidRuntime(2621): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1083)
08-08 16:25:45.655: E/AndroidRuntime(2621): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2064)
08-08 16:25:45.655: E/AndroidRuntime(2621): ... 11 more
どんな助けでも大歓迎です。私はそれを理解することができません.. 私の人生のために。
activity_main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<android.support.v4.view.ViewPager
android:id="@+id/pager"
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
</android.support.v4.view.ViewPager>
</RelativeLayout>
FragmentTab1 - scanlist.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<android.support.v4.view.ViewPager
android:id="@+id/pager"
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
</android.support.v4.view.ViewPager>
</RelativeLayout>
編集: これは修正されました!Talklol さん、正しい方法で考えさせてくれてありがとう。