切换到ListView时崩溃?

时间:2012-02-26 22:46:56

标签: android listview android-listview

LinearLayout的{​​{1}}正常,我的第二项活动是main.xml。我在ListView上有一个按钮,可以将我带到main.xml。我得到ListView并且在按下按钮时必须强行关闭。

main.xml中:

Unexpected Error message

Java for 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>

carlist.xml(ListView)活动:

    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);
            }
        });

    }
}

ListView活动的Java:

<?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>

logcat的:

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();
                };
          });       
       }
    }

甚至可以从常规视图转到ListView吗?

1 个答案:

答案 0 :(得分:2)

log中的关键字是:

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)

您正在尝试null pointer exception找到一些视图

我怀疑这是你的行1612

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

这应该在你的onCreate(),因为在那一点(现在它)它已经被启动了。


  

甚至可以从常规视图转到ListView吗?

确实如此。但是,如果您还没有了解它,我建议您检查ListActivity,以便轻松处理CarList项目中的整个列表数据和事件。


编辑 carlist.xml

中缺少按钮

ContentView 设置为carlist.xml

    setContentView(R.layout.carlist);

您需要在ImageView中使用名为 menuButton1 carlist.xml匹配,否则您将在以下调用中收到错误消息:

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

要避免错误,只需在ImageView中添加一个新的carlist.xml按钮(这次称之为其他内容,而不是 menuButton1