0

別のアクティビティに移動しようとすると、アプリがクラッシュします。これが私のソースコードです:

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): 

私の変数名が悪いことを知っています。できるだけ早く変更します。

編集:新しいCatLog:

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
4

3 に答える 3

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

ここでアプリがクラッシュします:

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

これviewは、TextViewではなくLinearLayoutであるためです。試す:

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)

22行目のSingleListItemを見ると、次のことがわかります。

txtProduct.setText(product);

だからtxtProductnullです。これはfindViewById(R.id.title)、IDを持つyorレイアウトでビューを見つけることができなかったことを意味します@+id/title。ロードしているビューを見てみましょう。

this.setContentView(R.layout.row);

うーん、(または)row.xmlのTextViewはありません。間違ったレイアウトをロードしました。次を使用してください。titleproduct_label2.xml

this.setContentView(R.layout.singleitemlist);

今、私はあなたのアプリをデバッグするすべてのステップをあなたに説明することはできません。しかし、上記の私のアドバイスから、LogCatの読み方を理解する必要があります。スタックトレース(LogCatエラー)は、問題に直接つながります。

チェックマークをクリックして、この回答を元の質問の解決策として受け入れてください。今後のエラーを解決できない場合は、新しい質問をしてください。幸運を!:)

于 2012-12-30T21:32:56.353 に答える
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からこれらの回答に到達した方法を理解していれば、これは非常に貴重なレッスンであり、今後、再度投稿する前に、さらに多くのエラーを自分で修正できるようになります:-)

于 2012-12-30T22:19:08.870 に答える
0

extrasそれがからデータを取得する方法かどうかはわかりませんIntent

これを試して:

Bundle extras = getIntent().getExtras();
if (extras != null) {
   product = extras.getString("product");
}
于 2012-12-30T22:11:05.640 に答える