2012-12-30 26 views
0

每當我嘗試去其他活動時,應用程序崩潰。下面是我的源代碼:當我試圖發送意圖到另一個活動時,應用程序崩潰(android)

Main.Java

public class Main extends ListActivity { 

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

     // storing string resources into Array 
     String[] adobe_products ; 
     adobe_products = getResources().getStringArray(R.array.adobe_products); 

     // Binding resources Array to ListAdapter 
     this.setListAdapter(new ArrayAdapter<String>(this, R.layout.row,R.id.weekofday, adobe_products)); 

     ListView lv = getListView(); 

     // listening to single list item on click 

     lv.setOnItemClickListener(new OnItemClickListener() { 
      public void onItemClick(AdapterView<?> parent, View view, 
       int position, long id) { 

       // selected item 
       String product = ((TextView) view).getText().toString(); 

       // Launching new Activity on selecting single List Item 
       Intent i = new Intent(getApplicationContext(), SingleListItem.class); 
       // sending data to new activity 
       i.putExtra("product", product); 
       startActivity(i); 

      } 
     }); 

    } 
} 

SingleListItem.java

public class SingleListItem extends Activity{ 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     this.setContentView(R.layout.row); 

     TextView txtProduct = (TextView) findViewById(R.id.title); 
     TextView txtProduct2 = (TextView) findViewById(R.id.product_label2); 
     Intent i = getIntent(); 
     // getting attached intent data 
     String product = (String) i.getStringExtra("product"); 
     // displaying selected product name 
     txtProduct.setText(product); 
     txtProduct.setTextColor(Color.RED); 

     if(product.equals("Odilo Globocnik")) 
     { 
      txtProduct2.setText(R.string.Odilo_Globocnik); 
      txtProduct2.setTextColor(Color.BLUE); 
     } 
     else if (product.equals("Josef Kramer")) 
     { 
      txtProduct2.setText(R.string.Josef_Kramer); 
      txtProduct2.setTextColor(Color.BLUE); 
     } 
     else if (product.equals("Paul Blobel")) 
     { 
      txtProduct2.setText(R.string.Paul_Blobel); 
      txtProduct2.setTextColor(Color.BLUE); 
     } 
     else if (product.equals("Friedrich Jeckeln")) 
     { 
      txtProduct2.setText(R.string.Friedrich_Jeckeln); 
      txtProduct2.setTextColor(Color.BLUE); 
     } 

    } 
} 

Row.XML

<?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="wrap_content" 
android:orientation="horizontal"> 
<ImageView 
android:id="@+id/icon" 
android:layout_width="wrap_content" 
android:layout_height="wrap_content" 
android:src="@drawable/ic_launcher"/> 
<TextView 
android:id="@+id/weekofday" 
android:layout_width="wrap_content" 
android:layout_height="wrap_content"/> 
</LinearLayout> 

Singleitemlist.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="horizontal" > 

    <TextView 
     android:id="@+id/title" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="TextView" /> 

    <TextView 
     android:id="@+id/product_label2" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:text="Medium Text" 
     android:textAppearance="?android:attr/textAppearanceMedium" /> 

</LinearLayout> 

main.xml中

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

    <TextView 
     android:id="@+id/textview" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
    /> 

    <ListView 
     android:id="@+id/list" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
    /> 
</LinearLayout> 

該清單:

<activity android:name=".SingleListItem" 
        android:label="Single Item Selected"> 

     </activity> 

的logcat:

12-30 23:13:55.129: E/AndroidRuntime(19016): Caused by: java.lang.RuntimeException: Your content must have a ListView whose id attribute is 'android.R.id.list' 
12-30 23:13:55.129: E/AndroidRuntime(19016): at android.app.ListActivity.onContentChanged(ListActivity.java:243) 
12-30 23:13:55.129: E/AndroidRuntime(19016): at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:210) 
12-30 23:13:55.129: E/AndroidRuntime(19016): at android.app.Activity.setContentView(Activity.java:1657) 
12-30 23:13:55.129: E/AndroidRuntime(19016): at com.example.booktest.Main.onCreate(Main.java:20) 
12-30 23:13:55.129: E/AndroidRuntime(19016): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047) 
12-30 23:13:55.129: E/AndroidRuntime(19016): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1617) 
12-30 23:13:55.129: E/AndroidRuntime(19016): ... 11 more 
12-30 23:15:35.189: W/dalvikvm(19093): threadid=1: thread exiting with uncaught exception (group=0x40018560) 


12-30 23:15:35.199: E/AndroidRuntime(19093): FATAL EXCEPTION: main 
12-30 23:15:35.199: E/AndroidRuntime(19093): java.lang.ClassCastException: android.widget.LinearLayout 
12-30 23:15:35.199: E/AndroidRuntime(19093): at com.example.booktest.Main$1.onItemClick(Main.java:40) 
12-30 23:15:35.199: E/AndroidRuntime(19093): at android.widget.AdapterView.performItemClick(AdapterView.java:284) 
12-30 23:15:35.199: E/AndroidRuntime(19093): at android.widget.ListView.performItemClick(ListView.java:3513) 
12-30 23:15:35.199: E/AndroidRuntime(19093): at android.widget.AbsListView$PerformClick.run(AbsListView.java:1812) 
12-30 23:15:35.199: E/AndroidRuntime(19093): at android.os.Handler.handleCallback(Handler.java:587) 
12-30 23:15:35.199: E/AndroidRuntime(19093): at android.os.Handler.dispatchMessage(Handler.java:92) 
12-30 23:15:35.199: E/AndroidRuntime(19093): at android.os.Looper.loop(Looper.java:130) 
12-30 23:15:35.199: E/AndroidRuntime(19093): at android.app.ActivityThread.main(ActivityThread.java:3737) 
12-30 23:15:35.199: E/AndroidRuntime(19093): 

我知道我的變量的名稱是壞的。我會盡快改變它們。

編輯:新目錄下載:

12-30 23:51:19.759: E/AndroidRuntime(19744): Caused by: java.lang.NullPointerException 
12-30 23:51:19.759: E/AndroidRuntime(19744): at com.example.booktest.SingleListItem.onCreate(SingleListItem.java:22) 
12-30 23:51:19.759: E/AndroidRuntime(19744): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047) 
12-30 23:51:19.759: E/AndroidRuntime(19744): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1617) 
12-30 23:51:19.759: E/AndroidRuntime(19744): ... 11 more 

回答

3
java.lang.ClassCastException: android.widget.LinearLayout 
    at com.example.booktest.Main$1.onItemClick(Main.java:40) 

您的應用程序崩潰這裏:

String product = ((TextView) view).getText().toString(); 

因爲view是沒有的LinearLayout一個TextView。嘗試:

String product = ((TextView) view.findViewById(R.id.weekOfDay)).getText().toString(); 

加成
你可以看到這是一個不同的logcat,這裏的問題是:

Caused by: java.lang.NullPointerException 
    at com.example.booktest.SingleListItem.onCreate(SingleListItem.java:22) 

如果你看一下SingleListItem第22行,你會發現:

txtProduct.setText(product); 

因此txtProduct爲空。這意味着findViewById(R.id.title)無法找到yor佈局中的任何視圖,其ID爲@+id/title。讓我們來看看查看您加載:

this.setContentView(R.layout.row); 

嗯,row.xml沒有爲title(或product_label2.xml)一個TextView。你裝錯了佈局,使用方法:

this.setContentView(R.layout.singleitemlist); 

現在我不能走,你通過調試您的應用程序的每一步。但從我的建議上面,你應該知道如何閱讀你的LogCat。堆棧跟蹤(LogCat錯誤)會導致您直接遇到問題。

請點擊複選標記以接受該答案作爲原問題的解決方案,如果您無法解決將來的錯誤,請提出一個新問題。祝你好運! :)

+0

仍然崩潰。感謝您的嘗試。 –

+0

請發佈新的LogCat錯誤。首先清除你的LogCat,然後再次運行應用程序,之後它崩潰只發布紅色錯誤行。 – Sam

+0

我用LogCat編輯了第一篇文章。 –

0

我不確定這是怎麼得到從Intentextras數據。

試試這個:

Bundle extras = getIntent().getExtras(); 
if (extras != null) { 
    product = extras.getString("product"); 
} 
+0

['Intent#getStringExtra()'](http://developer.android.com/reference/android/content/Intent.html#getStringExtra%28java.lang.String%29)是上面代碼的簡寫。但是在這兩種情況下'getIntent()'都可以返回null,所以你應該檢查它。 – Sam

1

薩姆的答案是100%正確的,你也有其他錯誤你Main活動。

Caused by: java.lang.RuntimeException: Your content must have a ListView whose id attribute is 'android.R.id.list' 
12-30 23:13:55.129: E/AndroidRuntime(19016): at android.app.ListActivity.onContentChanged(ListActivity.java:243) 

因爲您擴展了ListActivity,所以Android代碼正在尋找具有特定ID的ListView。您需要更改:

main.xml中

<ListView 
    android:id="@+id/list" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
/> 

這樣:

<ListView 
    android:id="@android:id/list" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
/> 

如果你瞭解我們如何得出這些答案從logcat的這是一個非常有價值的課程,你將能夠在將來需要再次發佈之前自行修復許多許多更多的錯誤:-)

+0

謝謝布倫戴爾。好東西要知道。 –

相關問題