Android startActivity () crash

The main disclaimer; I am new to programming mobile apps in general and Android in particular.

I have a button that, when clicked, should open the following operation:

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 placing "this" where "ctx" does not get context when inside onClick)

The program crashes before the current activity is obscured (although I'm not sure how this affects the transition). After commenting on almost everything, here is the activity that it causes:

 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 is the button code, but I can’t get any closer to the answer.

Edit: android manifest file has:

 <activity android:name=".DiceCustomList"></activity> 

Edit 2: Ah, after finally finding where Eclipse hides the stack, he told me the following: β€œYour content should have a ListView whose id attribute isβ€œ android.R.id.list ”, which they actually mean β€œ@ + id / android: list.” Well, that was interesting. (Edit3: By that I mean, that was the answer. Thanks for the tip.)

+4
source share
3 answers

Ensure that the DiceCustomList action has an entry in the XML manifest file.

+9
source

(where "private Context ctx = this;" because putting "this" where "ctx" did not get context when inside OnClick)

You can use YourActivity.this to get the context. The simple 'this' does not work, because inside this method it points to an OnClickListener object.

Can you give us LogCat error logs?

+1
source

The problem is that your DiceCustomList class extends ListActivity , but some required fragments are missing from the layout file.

The layout file for ListActivity must contain the following two elements with specific identifiers. The ListActivity class uses these elements to bind the 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} /> 

Android docs have a nice XML link and a cursor binding example: http://developer.android.com/reference/android/app/ListActivity.html

In general, you should familiarize yourself with adb and logcat . Due to the way the emulator debugs applications, your application will crash many times and you will simply see the message β€œSource not found” in Eclipse. The code of the packaging problem in the try / catch blocks and the registration of exceptions ( Log.e( TAG, MESSAGE); ) is a quick and easy way to see what is actually happening.

0
source

Source: https://habr.com/ru/post/1339920/


All Articles