Reputation: 498
I have a custom Listview that I want to try and implement in my SherlockFragment(Tab1).
In a normal extend activity it works great, but whenever I try and declare it in my SherlockFragmentActivity(MainActivity) - I get a NullPointerException.
My MainActivity looks like this:
@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);
{
My 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);
}
}
And my 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();
}
}
And the error looks like this:
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
Any help is greatly appreciated. I cant figure it out.. for the life of me.
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>
EDIT: This thing was FIXED! Thank you kindly, Talklol for getting me thinking the right way.
Upvotes: 0
Views: 366
Reputation: 12887
If there's one thing better than finding the solution for you, is helping you figure it out yourself. Good job!
Update: I got it working! Thanks Talklol you made me find the correct answer! I owe you a beer! If you post a nonsensepost Ill give you the right answer as a thanks! Again a billion thanks. (solution: I had to declare the listview in the fragment and then use the getactivity() as an argument!)
Upvotes: 1
Reputation: 6598
As Talkol pointed out in his comment, RecipeList is probably null. I cant see view with id mainListView
in activity_main.xml
so (ListView) findViewById(R.id.mainListView)
will return null;
Upvotes: 1
Reputation: 527
You need to remove the else part in the getView method of your adapter. The values for the views should be set irrespective of whether it has been inflated previously or should be inflated for the first time.
Upvotes: 1