【问题标题】:App is crashing when I try to send intent to another activity (android)当我尝试将意图发送到另一个活动(android)时,应用程序崩溃了
【发布时间】:2012-12-30 21:26:17
【问题描述】:

每当我尝试进行其他活动时,应用程序都会崩溃。这是我的源代码:

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

    }
}

行.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

【问题讨论】:

    标签: android eclipse android-intent android-listview


    【解决方案1】:
    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)
    

    如果您查看第 22 行的 SingleListItem,您会发现:

    txtProduct.setText(product);
    

    所以txtProduct 为空。这意味着findViewById(R.id.title) 无法在您的布局中找到 ID 为 @+id/title 的任何视图。让我们看看你正在加载什么视图:

    this.setContentView(R.layout.row);
    

    嗯,row.xml 没有 title(或 product_label2.xml)的 TextView。你加载了错误的布局,使用:

    this.setContentView(R.layout.singleitemlist);
    

    现在我无法引导您完成调试应用的每一步。但是根据我上面的建议,您应该了解如何阅读 LogCat。堆栈跟踪(LogCat 错误)将直接导致问题。

    请单击复选标记以接受此答案作为原始问题的解决方案,如果您无法解决未来的错误,请提出新问题。祝你好运! :)

    【讨论】:

    • 仍然崩溃。感谢您的尝试。
    • 请发布新的 LogCat 错误。首先清除您的 LogCat,然后再次运行该应用程序,在它崩溃后只发布红色错误行。
    • 很高兴我能帮上忙!我会给你最后一个提示,如果你想在 Main 类中使用 main.xml,你需要将 ListView 的 id 更改为 android:id="@android:id/list",就像 LogCats 暗示的那样。
    【解决方案2】:

    Sam 的回答是 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 得出这些答案,这是非常有价值的一课,您将能够在在您需要再次发布之前的未来:-)

    【讨论】:

    • 谢谢布伦德尔。好东西要知道。
    【解决方案3】:

    我不确定这是否是您从Intent 获取extras 数据的方式。

    试试这个:

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

    【讨论】:

    • Intent#getStringExtra() 是上述代码的简写。但在这两种情况下,getIntent() 都可能返回 null,所以你应该检查一下。
    猜你喜欢
    • 2021-11-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多