I am attempting to convert one of my apps to use Fragments to improve
how the app looks and works on ICS / Honeycomb but I've come across a
problem which although I've spent many hours Googleing and reading
answers on here I can't seem to find a fix for.
I have a custom list adapter which is used to display some info I get
from an sqllite db. It worked fine before I tried to convert to
fragments when it was being displayed in an activity.
However when I try and put the ListView into a fragment the app FC
with the following error:
W/dalvikvm( 540): threadid=1: thread exiting with uncaught
exception (group=0x4
09951f8)
E/AndroidRuntime( 540): FATAL EXCEPTION: main
E/AndroidRuntime( 540): java.lang.NullPointerException
E/AndroidRuntime( 540): at
com.msi.shortwave.FreqAdaptor.getView(FreqAda
ptor.java:43)
E/AndroidRuntime( 540): at
android.widget.AbsListView.obtainView(AbsList
View.java:2033)
E/AndroidRuntime( 540): at
android.widget.ListView.makeAndAddView(ListVi
ew.java:1772)
E/AndroidRuntime( 540): at
android.widget.ListView.fillDown(ListView.jav
a:672)
E/AndroidRuntime( 540): at
android.widget.ListView.fillFromTop(ListView.
java:732)
E/AndroidRuntime( 540): at
android.widget.ListView.layoutChildren(ListVi
ew.java:1625)
E/AndroidRuntime( 540): at
android.widget.AbsListView.onLayout(AbsListVi
ew.java:1863)
E/AndroidRuntime( 540): at
android.view.View.layout(View.java:11158)
E/AndroidRuntime( 540): at
android.view.ViewGroup.layout(ViewGroup.java:
4197)
E/AndroidRuntime( 540): at
android.widget.LinearLayout.setChildFrame(Lin
earLayout.java:1628)
E/AndroidRuntime( 540): at
android.widget.LinearLayout.layoutVertical(Li
nearLayout.java:1486)
E/AndroidRuntime( 540): at
android.widget.LinearLayout.onLayout(LinearLa
yout.java:1399)
E/AndroidRuntime( 540): at
android.view.View.layout(View.java:11158)
E/AndroidRuntime( 540): at
android.view.ViewGroup.layout(ViewGroup.java:
4197)
E/AndroidRuntime( 540): at
android.widget.LinearLayout.setChildFrame(Lin
earLayout.java:1628)
E/AndroidRuntime( 540): at
android.widget.LinearLayout.layoutHorizontal(
LinearLayout.java:1617)
E/AndroidRuntime( 540): at
android.widget.LinearLayout.onLayout(LinearLa
yout.java:1401)
E/AndroidRuntime( 540): at
android.view.View.layout(View.java:11158)
E/AndroidRuntime( 540): at
android.view.ViewGroup.layout(ViewGroup.java:
4197)
E/AndroidRuntime( 540): at
android.widget.LinearLayout.setChildFrame(Lin
earLayout.java:1628)
E/AndroidRuntime( 540): at
android.widget.LinearLayout.layoutVertical(Li
nearLayout.java:1486)
E/AndroidRuntime( 540): at
android.widget.LinearLayout.onLayout(LinearLa
yout.java:1399)
E/AndroidRuntime( 540): at
android.view.View.layout(View.java:11158)
E/AndroidRuntime( 540): at
android.view.ViewGroup.layout(ViewGroup.java:
4197)
E/AndroidRuntime( 540): at
android.widget.FrameLayout.onLayout(FrameLayo
ut.java:431)
E/AndroidRuntime( 540): at
android.view.View.layout(View.java:11158)
E/AndroidRuntime( 540): at
android.view.ViewGroup.layout(ViewGroup.java:
4197)
E/AndroidRuntime( 540): at
android.widget.LinearLayout.setChildFrame(Lin
earLayout.java:1628)
E/AndroidRuntime( 540): at
android.widget.LinearLayout.layoutVertical(Li
nearLayout.java:1486)
E/AndroidRuntime( 540): at
android.widget.LinearLayout.onLayout(LinearLa
yout.java:1399)
E/AndroidRuntime( 540): at
android.view.View.layout(View.java:11158)
E/AndroidRuntime( 540): at
android.view.ViewGroup.layout(ViewGroup.java:
4197)
E/AndroidRuntime( 540): at
android.widget.FrameLayout.onLayout(FrameLayo
ut.java:431)
E/AndroidRuntime( 540): at
android.view.View.layout(View.java:11158)
E/AndroidRuntime( 540): at
android.view.ViewGroup.layout(ViewGroup.java:
4197)
E/AndroidRuntime( 540): at
android.view.ViewRootImpl.performTraversals(V
iewRootImpl.java:1462)
E/AndroidRuntime( 540): at
android.view.ViewRootImpl.handleMessage(ViewR
ootImpl.java:2382)
E/AndroidRuntime( 540): at
android.os.Handler.dispatchMessage(Handler.ja
va:99)
E/AndroidRuntime( 540): at
android.os.Looper.loop(Looper.java:137)
E/AndroidRuntime( 540): at
android.app.ActivityThread.main(ActivityThrea
d.java:4340)
E/AndroidRuntime( 540): at
java.lang.reflect.Method.invokeNative(Native
Method)
E/AndroidRuntime( 540): at
java.lang.reflect.Method.invoke(Method.java:5
11)
E/AndroidRuntime( 540): at
com.android.internal.os.ZygoteInit$MethodAndA
rgsCaller.run(ZygoteInit.java:784)
E/AndroidRuntime( 540): at
com.android.internal.os.ZygoteInit.main(Zygot
eInit.java:551)
E/AndroidRuntime( 540): at
dalvik.system.NativeStart.main(Native Method)
W/ActivityManager( 84): Force finishing activity
com.msi.shortwave/.main
The fragment (results.xml) layout is :
<?xml version="1.0" encoding="utf-8"?><LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<ListView android:id="@+id/ListView01"
android:layout_width="fill_parent"
android:layout_height="100dp"
android:layout_weight="0"
android:cacheColorHint="#00000000"></ListView>
</LinearLayout>
My row.xml file used by the custom list adaptor is as follows
(android:text entries are just placeholders, this is replaced by the
app for stuff like the station name)
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout android:id="@+id/LinearLayout01"
android:layout_width="fill_parent" xmlns:android="http://
schemas.android.com/apk/res/android"
android:paddingLeft="10sp" android:paddingRight="10sp"
android:layout_height="20dp"
android:orientation="vertical" android:paddingTop="2sp">
<TextView android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:id="@+id/tvStationname"
android:text="BBC World Service" android:typeface="sans"
android:textSize="16sp" android:textColor="#990200"
android:textStyle="bold"></TextView>
<LinearLayout android:id="@+id/LinearLayout02"
android:layout_height="wrap_content"
android:layout_width="fill_parent">
<TextView android:id="@+id/TextView01"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textStyle="bold"
android:text="Start:" android:textSize="13sp"
android:textColor="#000000" android:paddingRight="5sp"></TextView>
<TextView android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="00:00"
android:textColor="#000000"
android:id="@+id/tvStarttime" android:textSize="13sp"
></TextView>
<TextView android:id="@+id/TextView02"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textStyle="bold"
android:textColor="#000000"
android:text="End:" android:textSize="13sp"
android:paddingLeft="5sp" android:paddingRight="5sp"></TextView>
<TextView android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="01:00"
android:textColor="#000000"
android:id="@+id/tvEndtime"
android:textSize="13sp"></TextView>
<TextView android:id="@+id/TextView03"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textStyle="bold"
android:text="Target Area:" android:textSize="13sp"
android:textColor="#000000"
android:paddingLeft="5sp"
android:paddingRight="5sp"></TextView>
<TextView android:id="@+id/tvTarget"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#000000"
android:text="LA, NA"
android:textSize="13sp"></TextView>
</LinearLayout>
<LinearLayout android:id="@+id/LinearLayout02"
android:layout_height="wrap_content"
android:layout_width="fill_parent">
<TextView android:id="@+id/TextView01"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textStyle="bold"
android:textColor="#000000"
android:text="Frequencies "
android:textSize="13sp"></TextView>
<HorizontalScrollView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:scrollbars ="none"
>
<TextView android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="12312, 12314,
14131, 12312"
android:id="@+id/tvFreqs" android:textColor="#000000"
android:textSize="13sp" android:layout_weight="1"
android:singleLine="true" ></TextView>
</HorizontalScrollView>
</LinearLayout>
</LinearLayout>
My list adapter is called by
list = (ListView) v.findViewById(R.id.ListView01);
list.setAdapter(adapter);
The code in my customlistview is FreqAdaptor.java - the crash is in
"public View getView" and is shown by comments:
package com.msi.shortwave;
import java.util.List;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.TextView;
public class FreqAdaptor extends BaseAdapter implements
OnClickListener {
private Context context;
private LayoutInflater inflate;
private List<Freqlist> listFreq;
public FreqAdaptor(Context context, List<Freqlist> listFreq,
LayoutInflater inflate) {
this.context = context;
this.listFreq = listFreq;
this.inflate = inflate;
}
public int getCount() {
return listFreq.size();
}
public Object getItem(int position) {
return listFreq.get(position);
}
public long getItemId(int position) {
return position;
}
public View getView(int position, View convertView, ViewGroup
viewGroup) {
Freqlist entry = listFreq.get(position);
if (convertView == null) {
convertView = inflate.inflate(R.layout.row, null);
//crash is here. inflate is the one used by the view on my
results.java file.
// I have also tried with LayoutInflater inflate =
(LayoutInflater) context
// .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
but I get the
//same result
}
TextView tvStationname = (TextView)
convertView.findViewById(R.id.tvStationname);
tvStationname.setText(entry.getStationname());
TextView tvStarttime = (TextView)
convertView.findViewById(R.id.tvStarttime);
tvStarttime.setText(entry.getStarttime());
TextView tvEndtime = (TextView)
convertView.findViewById(R.id.tvEndtime);
tvEndtime.setText(entry.getEndtime());
TextView tvTarget = (TextView)
convertView.findViewById(R.id.tvTarget);
tvTarget.setText(entry.getTargets());
TextView tvFreqs = (TextView)
convertView.findViewById(R.id.tvFreqs);
tvFreqs.setText(entry.getFreqs());
return convertView;
}
@Override
public void onClick(View view) {
Freqlist entry = (Freqlist) view.getTag();
}
}
My results.java file starts with the following. As mentioned I have
tried using the same inflater for my FreqAdaptor.java and have tried
making a new inflater but I get the same crash.
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup
container, Bundle savedInstanceState) {
// Inflate the layout for this fragment
v = inflater.inflate(R.layout.results, container, false);
Bundle bund1 = getArguments();
passinflate = inflater;
String time = bund1.getString("time");
String target = bund1.getString("target");
String station = bund1.getString("station");
String freq = bund1.getString("freq");
new searchdb().execute(time,target,station,freq);
return v;
}
Can anyone help? As I mention this works fine without using Fragments.
App is here https://market.android.com/details?id=com.msi.shortwave
and I can provide the full source if anyone needs that to provide me
with some suggestions as I really am stuck with it.
Some of my indentation has been messed up by posting using [code]
sections above. Not sure why.
EDIT - Do my XML files look OK? I've even changed to a ListFragment
and used the xml file here for my fragment- developer.android.com/
reference/android/app/ListFragment.html but the same thing keeps
happening. Anyone got any ideas?
--
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en