Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7595787
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T21:45:30+00:00 2026-05-30T21:45:30+00:00

I have a normal LinearLayout for my main.xml , and my second activity is

  • 0

I have a normal LinearLayout for my main.xml, and my second activity is a ListView. I have a button on main.xml that should take me to the ListView. I get an Unexpected Error message and have to force close when I press the button.

Main.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"
    android:weightSum="1"
    android:background="@drawable/forzabg">

        <ImageView
            android:id="@+id/mainlogo"
            android:layout_width="210dp"
            android:layout_height="119dp"
            android:layout_gravity="center"
            android:layout_weight="0.00"
            android:scaleType="fitXY"
            android:src="@drawable/forzalogo" >

 </ImageView>

        <ImageView
            android:id="@+id/menuButton1"
            android:layout_width="185dp"
            android:layout_height="56dp"
            android:layout_gravity="center"
            android:layout_weight="0.14"
            android:src="@drawable/carslist" />

 />

</LinearLayout>

Java for main.xml:

    package com.forza;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;

public class Forza2Activity extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        ImageView carbutton = (ImageView) findViewById(R.id.menuButton1);

        carbutton.setOnClickListener(new View.OnClickListener() {
            public void onClick(View view) {
                Intent myIntent = new Intent(view.getContext(), CarList.class);
                startActivity(myIntent);
            }
        });

    }
}

carlist.xml (ListView) activity:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" 
    android:orientation="vertical">

    <ListView
        android:id="@+id/mylist"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:entries="@array/Brands" >
    </ListView>

</LinearLayout>

Java for ListView activity:

package com.forza;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.ListView;


    public class CarList extends Activity {



        ListView carList = (ListView) findViewById(R.id.mylist);
            protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.carlist);

            ImageView carbutton = (ImageView) findViewById(R.id.menuButton1);

            carbutton.setOnClickListener(new View.OnClickListener() {
                public void onClick(View view) {
                    Intent intent = new Intent();
                    setResult(RESULT_OK, intent);
                    finish();
                };
          });       
       }
    }

LogCat:

02-26 16:35:50.158: W/dalvikvm(341): threadid=3: thread exiting with uncaught exception (group=0x4001b188)
02-26 16:35:50.158: E/AndroidRuntime(341): Uncaught handler: thread main exiting due to uncaught exception
02-26 16:35:50.168: E/AndroidRuntime(341): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.forza/com.forza.CarList}: java.lang.NullPointerException
02-26 16:35:50.168: E/AndroidRuntime(341):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2417)
02-26 16:35:50.168: E/AndroidRuntime(341):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2512)
02-26 16:35:50.168: E/AndroidRuntime(341):  at android.app.ActivityThread.access$2200(ActivityThread.java:119)
02-26 16:35:50.168: E/AndroidRuntime(341):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1863)
02-26 16:35:50.168: E/AndroidRuntime(341):  at android.os.Handler.dispatchMessage(Handler.java:99)
02-26 16:35:50.168: E/AndroidRuntime(341):  at android.os.Looper.loop(Looper.java:123)
02-26 16:35:50.168: E/AndroidRuntime(341):  at android.app.ActivityThread.main(ActivityThread.java:4363)
02-26 16:35:50.168: E/AndroidRuntime(341):  at java.lang.reflect.Method.invokeNative(Native Method)
02-26 16:35:50.168: E/AndroidRuntime(341):  at java.lang.reflect.Method.invoke(Method.java:521)
02-26 16:35:50.168: E/AndroidRuntime(341):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860)
02-26 16:35:50.168: E/AndroidRuntime(341):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
02-26 16:35:50.168: E/AndroidRuntime(341):  at dalvik.system.NativeStart.main(Native Method)
02-26 16:35:50.168: E/AndroidRuntime(341): Caused by: java.lang.NullPointerException
02-26 16:35:50.168: E/AndroidRuntime(341):  at android.app.Activity.findViewById(Activity.java:1612)
02-26 16:35:50.168: E/AndroidRuntime(341):  at com.forza.CarList.<init>(CarList.java:15)
02-26 16:35:50.168: E/AndroidRuntime(341):  at java.lang.Class.newInstanceImpl(Native Method)
02-26 16:35:50.168: E/AndroidRuntime(341):  at java.lang.Class.newInstance(Class.java:1479)
02-26 16:35:50.168: E/AndroidRuntime(341):  at android.app.Instrumentation.newActivity(Instrumentation.java:1021)
02-26 16:35:50.168: E/AndroidRuntime(341):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2409)
02-26 16:35:50.168: E/AndroidRuntime(341):  ... 11 more
02-26 16:35:50.178: I/dalvikvm(341): threadid=7: reacting to signal 3
02-26 16:35:50.178: E/dalvikvm(341): Unable to open stack trace file '/data/anr/traces.txt': Permission denied
02-26 16:35:53.198: I/Process(341): Sending signal. PID: 341 SIG: 9
02-26 16:35:53.608: D/dalvikvm(348): GC freed 611 objects / 48680 bytes in 58ms
02-26 16:35:54.258: D/dalvikvm(348): GC freed 59 objects / 2336 bytes in 50ms

Is it even possible to go from a regular view to a ListView?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T21:45:31+00:00Added an answer on May 30, 2026 at 9:45 pm

    The key line in the log is this :

    02-26 16:35:50.168: E/AndroidRuntime(341): Caused by: java.lang.NullPointerException
    02-26 16:35:50.168: E/AndroidRuntime(341):  at android.app.Activity.findViewById(Activity.java:1612)
    

    You are getting a null pointer exception trying to find some view.

    I suspect this is your line 1612:

      ListView carList = (ListView) findViewById(R.id.mylist);
    

    This should be inside your onCreate() because at that point (where it is now) it wonT have been initilized yet.


    Is it even possible to go from a regular view to a ListView?

    It sure is. But in case you donT know about it already, I also suggest you should check the ListActivity which would allow easy handling of the entire list data and events on the items of your CarList.


    EDIT Missing button in the carlist.xml

    As you set your ContentView as the carlist.xml with the line

        setContentView(R.layout.carlist);
    

    You need to have a matching ImageView called menuButton1 in the carlist.xml, otherwise you ll get an error in the following call:

     ImageView carbutton = (ImageView) findViewById(R.id.menuButton1);
    

    To avoid the error, just add a new ImageView button in the carlist.xml (this time call it something else, not menuButton1)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have row for ListView(listview is inside LinearLayout) like <?xml version=1.0 encoding=utf-8?> <RelativeLayout xmlns:android=http://schemas.android.com/apk/res/android
I have a normal asp.net page containing some code that I want to measure
I have read that 'Normal' ARM instructions are fixed length - 32 bits. And
Is it normal to have tests that are way bigger than the actual code
I have an app that normal users need to be able to run, but
I have a crash (Application Stopped Unexpectedly) problem with this main.xml is a HelloWorld
I have the normal PayPal donation button. I want to add two multiple choice
I have an layout <?xml version=1.0 encoding=utf-8?> <LinearLayout xmlns:android=http://schemas.android.com/apk/res/android android:orientation=horizontal android:layout_width=fill_parent android:layout_height=fill_parent android:background=#FFFFFF> <TextView
I have a ScrollView that contains a complex LinearLayout with various elements, among which
I have this layout in xml. And when I run the activity at the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.