0

I'm working on an app where I want the logo left justified and then a list of categories on the right side. So what I have is the logo in an image view which is in the Linear Layout and then I add a ListView to the LinearLayout too so they can be on the same activity. When I try to run it, I get a stopped unexpectedly error.

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

    Commands.setText("Commands");
    LinearLayout LL = new LinearLayout(this);

    ImageView Logo = (ImageView)findViewById(0x7f020000);
    Logo.setImageDrawable(getResources().getDrawable(android.R.drawable.ic_menu_myplaces));

    ListView Cats = new ListView(this);

    String Categories[] = new String[3];
    Categories[0] = "Hardware";
    Categories[1] = "Language";
    Categories[2] = "Libraries";
    ArrayAdapter<String> list = new ArrayAdapter<String>(this, 0, 2, Categories);

    Cats.setAdapter(list);
    LL.addView(Logo);
    LL.addView(Cats);
    setContentView(LL);

}

Any help, I'm really confused

Andro Selva
  • 53,910
  • 52
  • 193
  • 240
Funkyguy
  • 628
  • 2
  • 10
  • 31

2 Answers2

0

Ok. I think the problem is, you haven't called setContentView() in your onCreate().

So obviously, Android doesn't know where to look for ImageView with that id and you will run into Null Pointer most probably.

Add setContentView() to your onCreate() before thsi line,

 ImageView Logo = (ImageView)findViewById(0x7f020000);
Andro Selva
  • 53,910
  • 52
  • 193
  • 240
0

The easiest way to create layouts in Android is to create a layout in a res/layout folder in xml and inflate it when you load your onCreate method. For example this xml might accomplish what you are looking for:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
     android:layout_width="fill_parent"
     android:layout_height="fill_parent"
     android:orientation="horizontal" >
    <ImageView
      android:id="@+id/(name for image view)"
      android:layout_width="(some size)"
      android:layout_height="(some size)"
     >
    </ImageView>

    <ListView
      android:id="@+id/(name for list view)"
      android:layout_width="(some size)"
      android:layout_height="fill_parent">
    </ListView>
<LinearLayout>

You should be able to find a lot tutorials on creating layouts with xml.

Ben Schwab
  • 26
  • 1
  • 3