Reputation: 41
Basic disclaimer; I am new to mobile app programming in general, and android in particular.
I have a button that, when clicked, should open the next activity:
bCustom.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
Intent i = new Intent(ctx, DiceCustomList.class);
startActivity(i);
}
});
(where "private Context ctx = this;" because putting "this" where "ctx" is did not get the context when inside onClick)
The program crashes before the current activity is obscured (though I'm not sure how transitions affect this). After commenting out nearly everything, here's the activity it's calling:
public class DiceCustomList extends ListActivity {
@Override
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.custom_list);
}
}
And custom_list.xml:
<?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">
<TextView android:text="@string/rollText"
android:id="@+id/textRoll2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textSize="11pt"
android:gravity="center"/>
<ListView android:layout_height="wrap_content"
android:id="@+id/listView1"
android:layout_width="fill_parent"/>
</LinearLayout>
I think the problem lies in the button code, but I can't seem to get closer to the answer than that.
Edit: The android manifest file does have:
<activity android:name=".DiceCustomList"></activity>
Edit 2: Ah, after finally finding where Eclipse hides the stacktrace, it told me this: "Your content must have a ListView whose id attribute is 'android.R.id.list'", by which they actually mean "@+id/android:list". Well, that was interesting. (Edit3: By which I mean, it was the answer. Thanks for the prompting.)
Upvotes: 4
Views: 8598
Reputation: 8528
The problem is that your DiceCustomList
class is extending ListActivity
but the layout file is missing some required pieces.
The layout file for a ListActivity
must have the following 2 elements with specific IDs. The ListActivity
class uses these elements to bind a data adapter to the list in your activity (using setListAdapter(myAdapter);
);
<ListView android:id="@+id/android:list"
{other attributes ommitted} />
<TextView android:id="@+id/android:empty"
{other attributes ommitted} />
The Android docs have good reference XML and example of binding a cursor: http://developer.android.com/reference/android/app/ListActivity.html
In general though, you should get familiar with adb
and logcat
. Due to the way the emulator debugs apps, many times your app will crash and you'll just see a "Source not found" message in Eclipse. Wrapping problem code in try/catch blocks and logging the exceptions ( Log.e( TAG, MESSAGE);
) is a quick & easy way to see what's really going on.
Upvotes: 0
Reputation: 1778
(where "private Context ctx = this;" because putting "this" where "ctx" is did not get the context when inside onClick)
You may use YourActivity.this to get the context. Simple 'this' does not work because inside this method it points to OnClickListener object.
Can you please give us a LogCat error logs?
Upvotes: 1
Reputation: 48871
Check to make sure the DiceCustomList activity has an entry in the manifest XML file.
Upvotes: 9