2012-02-26 78 views
0

我有一個正常的LinearLayoutmain.xml,我的第二個活動是ListView。我有一個main.xml的按鈕,應該帶我到ListView。我收到一個Unexpected Error message,並且在按下按鈕時必須強行關閉。切換到ListView時崩潰?

的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進行的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控件)活動:

<?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進行的ListView活動:

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 

它甚至有可能從常規視圖到ListView去?

回答

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(),因爲在這一點(它現在是)它不會有尚未initilized。


它甚至有可能從一個普通視圖到ListView去?

這肯定是。但是,如果您不知道它,我還建議您檢查ListActivity,以便您輕鬆處理CarList項目上的整個列表數據和事件。


編輯缺少在carlist.xml

按鈕,當您設置內容查看carlist.xml與線

setContentView(R.layout.carlist); 

你需要有一個匹配的ImageView稱爲menuButton1carlist.xml,否則你會得到一個在下面的調用n錯誤:

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

要避免錯誤,只是在carlist.xml添加新ImageView按鈕(這個時間打電話別的東西,不是menuButton1

+1

同意。另外,carlist xml中沒有menubutton1。 – ABentSpoon 2012-02-26 22:55:18

+0

是的,我想知道這是否因爲簡潔省略? @Cole – 2012-02-26 22:56:49

+0

@ABentSpoon這可能是老,但我以下[此](http://www.warriorpoint.com/blog/2009/05/24/android-how-to-switch-between-activities/)教程關於交換活動。它在Activity2中引用Activity1中的按鈕,所以我想這就是我必須做的。我對Android相當陌生。仍在學習!我不需要第二個活動的Java代碼中的那部分代碼? – Cole 2012-02-26 23:06:53