0

I'm trying to create a simple freq asked questions app. The first activity is supposed to display a listview of all the questions in the string-array "questions". The second activity is supposed to open when a question is clicked, and display the answer to the question from the string-array "answers". (I followed this guide about passing data between activities.)

The program compiles and doesn't show any errors in eclipse, but I keep getting the "program closed unepectedly" error when I try to run it. Not sure what the problem is.

Questions activity:

public class FreqActivity extends ListActivity {

    String[] ans;
    String[] questions;
    View tempView = null;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        ans = getResources().getStringArray(R.array.answers);
        questions = getResources().getStringArray(R.array.questions);

        setListAdapter(ArrayAdapter.createFromResource(getApplicationContext(),
               R.array.questions, R.layout.main));

    }

    public void onListItemClick(ListView l, View v, int position, long id) {
        //...

 }
}

Logcat error:

09-21 20:21:31.722: D/dalvikvm(333): GC_EXTERNAL_ALLOC freed 51K, 53% free 2564K/5379K, external 2108K/2137K, paused 66ms
09-21 20:21:31.792: E/ArrayAdapter(333): You must supply a resource ID for a TextView
09-21 20:21:31.792: D/AndroidRuntime(333): Shutting down VM
09-21 20:21:31.792: W/dalvikvm(333): threadid=1: thread exiting with uncaught exception (group=0x40015560)
09-21 20:21:31.913: E/AndroidRuntime(333): FATAL EXCEPTION: main
09-21 20:21:31.913: E/AndroidRuntime(333): java.lang.IllegalStateException: ArrayAdapter requires the resource ID to be a TextView
09-21 20:21:31.913: E/AndroidRuntime(333):  at android.widget.ArrayAdapter.createViewFromResource(ArrayAdapter.java:347)
09-21 20:21:31.913: E/AndroidRuntime(333):  at android.widget.ArrayAdapter.getView(ArrayAdapter.java:323)
09-21 20:21:31.913: E/AndroidRuntime(333):  at android.widget.AbsListView.obtainView(AbsListView.java:1430)
09-21 20:21:31.913: E/AndroidRuntime(333):  at android.widget.ListView.makeAndAddView(ListView.java:1745)
09-21 20:21:31.913: E/AndroidRuntime(333):  at android.widget.ListView.fillDown(ListView.java:670)
09-21 20:21:31.913: E/AndroidRuntime(333):  at android.widget.ListView.fillFromTop(ListView.java:727)
09-21 20:21:31.913: E/AndroidRuntime(333):  at android.widget.ListView.layoutChildren(ListView.java:1598)
09-21 20:21:31.913: E/AndroidRuntime(333):  at android.widget.AbsListView.onLayout(AbsListView.java:1260)
09-21 20:21:31.913: E/AndroidRuntime(333):  at android.view.View.layout(View.java:7175)
09-21 20:21:31.913: E/AndroidRuntime(333):  at android.widget.FrameLayout.onLayout(FrameLayout.java:338)
09-21 20:21:31.913: E/AndroidRuntime(333):  at android.view.View.layout(View.java:7175)
09-21 20:21:31.913: E/AndroidRuntime(333):  at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1254)
09-21 20:21:31.913: E/AndroidRuntime(333):  at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1130)
09-21 20:21:31.913: E/AndroidRuntime(333):  at android.widget.LinearLayout.onLayout(LinearLayout.java:1047)
09-21 20:21:31.913: E/AndroidRuntime(333):  at android.view.View.layout(View.java:7175)
09-21 20:21:31.913: E/AndroidRuntime(333):  at android.widget.FrameLayout.onLayout(FrameLayout.java:338)
09-21 20:21:31.913: E/AndroidRuntime(333):  at android.view.View.layout(View.java:7175)
09-21 20:21:31.913: E/AndroidRuntime(333):  at android.view.ViewRoot.performTraversals(ViewRoot.java:1140)
09-21 20:21:31.913: E/AndroidRuntime(333):  at android.view.ViewRoot.handleMessage(ViewRoot.java:1859)
09-21 20:21:31.913: E/AndroidRuntime(333):  at android.os.Handler.dispatchMessage(Handler.java:99)
09-21 20:21:31.913: E/AndroidRuntime(333):  at android.os.Looper.loop(Looper.java:123)
09-21 20:21:31.913: E/AndroidRuntime(333):  at android.app.ActivityThread.main(ActivityThread.java:3683)
09-21 20:21:31.913: E/AndroidRuntime(333):  at java.lang.reflect.Method.invokeNative(Native Method)
09-21 20:21:31.913: E/AndroidRuntime(333):  at java.lang.reflect.Method.invoke(Method.java:507)
09-21 20:21:31.913: E/AndroidRuntime(333):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
09-21 20:21:31.913: E/AndroidRuntime(333):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
09-21 20:21:31.913: E/AndroidRuntime(333):  at dalvik.system.NativeStart.main(Native Method)
09-21 20:21:31.913: E/AndroidRuntime(333): Caused by: java.lang.ClassCastException: android.widget.LinearLayout
09-21 20:21:31.913: E/AndroidRuntime(333):  at android.widget.ArrayAdapter.createViewFromResource(ArrayAdapter.java:340)
09-21 20:21:31.913: E/AndroidRuntime(333):  ... 26 more
09-21 20:23:04.262: I/Process(333): Sending signal. PID: 333 SIG: 9
Community
  • 1
  • 1
mdegges
  • 963
  • 3
  • 18
  • 39
  • 1
    By "no errors in Eclipse", do you also mean the LogCat is blank? 'Cuz if it's not, it'll help greatly to see the error text from it. – Cat Sep 21 '12 at 20:04
  • 1
    check logcat for the error and post it please – petey Sep 21 '12 at 20:05
  • also try to cut down the amount of code you're pasting, this is a large amount... – Kristopher Micinski Sep 21 '12 at 20:05
  • posted the logcat error. @KristopherMicinski, wanted it to be comprehensive so that the problem can be solved – mdegges Sep 21 '12 at 20:06
  • @mdegges that is usually the excuse for "I cannot isolate my error so I'm just posting everything." – Kristopher Micinski Sep 21 '12 at 20:07
  • ... that's all of the red error text from the LogCat? There should be a lot more. – Cat Sep 21 '12 at 20:07
  • @Eric that isn't even a logcat trace, it's just an error from logcat saying that logcat can't be started because the OP hasn't selected his device properly... – Kristopher Micinski Sep 21 '12 at 20:08
  • @KristopherMicinski Indeed so, I had just assumed he had corrected that following that output, since he did at some point run the app. Perhaps a bad assumption. – Cat Sep 21 '12 at 20:09
  • yes, that's the reason I'm posting here, I don't know what the problem is). I posted the updated logcat trace: "E/AndroidRuntime(333): Caused by: java.lang.ClassCastException: android.widget.LinearLayout" – mdegges Sep 21 '12 at 20:47

1 Answers1

0

The line

setListAdapter(ArrayAdapter.createFromResource(getApplicationContext(),
               R.array.questions, R.layout.main));

Should be

setListAdapter(ArrayAdapter.createFromResource(getApplicationContext(),
               R.array.questions, android.R.layout.simple_spinner_item));

Reference

Michaeldcooney
  • 1,435
  • 10
  • 14
  • No.. The first activity uses main.xml and displays all the questions, and the second activity is opened on-click, and displays just one answer in a textview. the layout file it uses is answers.xml – mdegges Sep 21 '12 at 20:53
  • 1
    No you should setContentView(R.layout.main) on your main activity, you don't pass R.layout.main as the id of the textView, I did make a mistake though and am editing my answer. – Michaeldcooney Sep 21 '12 at 21:01
  • There isn't a textview in main.xml, it's a listview. Please look at the code. – mdegges Sep 21 '12 at 21:05
  • 1
    I understand that, The error "ArrayAdapter requires the resource ID to be a TextView" is caused because you are passing main.xml which is not the resource id for a textview, do you understand? – Michaeldcooney Sep 21 '12 at 21:16
  • I think I'm not understanding how to use ID's correctly. The textview ID is "android:id="@android:id/text1", but when I follow your suggestion I get an error saying it doesn't recognize the id. Also when I change text1 to textview it isn't recognized in the java code or the xml file. – mdegges Sep 21 '12 at 21:39
  • oh yes that works! Thank you. Now it is just crashing on click. – mdegges Sep 21 '12 at 22:00
  • Ok, I found the second problem! When I declared "TextView tv = (TextView)" I didn't add "findViewById(R.id.id_of_txtview_in_answers.xml)" - so no text was appearing at all. – mdegges Sep 22 '12 at 01:55