0

我已尽一切努力使我的代码正常工作,但我无法弄清楚问题出在哪里。应用程序在首次运行时崩溃。

这是我的代码:

package com.example.smsTest;

import java.util.List;

import android.app.ListActivity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.Toast;

public class Messages extends ListActivity {
    Button btnNew;
    private SQLiteAdapter mySQLiteAdapter;

    private BroadcastReceiver mIntentReceiver;
    ListView listview;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.messages);

        btnNew = (Button) findViewById(R.id.btnNew);

        mySQLiteAdapter = new SQLiteAdapter(this);
        mySQLiteAdapter.openToRead();

        List<Message> values = mySQLiteAdapter.getAllMessages();

        ArrayAdapter<Message> adapter = new ArrayAdapter<Message>(this,
                    R.layout.messages, values);

        setListAdapter(adapter);

        btnNew.setOnClickListener(new View.OnClickListener() {
            public void onClick(View view) {
                Intent myIntent = new Intent(Messages.this, SMSTest.class);
                startActivity(myIntent);
            }

        });

    }

    @Override
    protected void onListItemClick(ListView l, View v, int position, long id) {
        String item = (String) getListAdapter().getItem(position);
        Toast.makeText(this, item + " selected", Toast.LENGTH_LONG).show();
    }



    @Override
    protected void onResume() {
        super.onResume();

        IntentFilter intentFilter = new IntentFilter("SmsMessage.intent.MAIN");
        mIntentReceiver = new BroadcastReceiver() {
            @Override
            public void onReceive(Context context, Intent intent) {
                @SuppressWarnings("unchecked")
                ArrayAdapter<Message> adapter = (ArrayAdapter<Message>) getListAdapter();
                Message message = null;

                String msg = intent.getStringExtra("get_msg");

                // Process the sms format and extract body & phoneNumber
                msg = msg.replace("\n", "");
                String body = msg.substring(msg.lastIndexOf(":") + 1,
                        msg.length());
                String sender = msg.substring(0, msg.lastIndexOf(":"));

                message = mySQLiteAdapter.createMessage(sender, body);
                // adapter.add(message);
                adapter.notifyDataSetChanged();
            }
        };

        this.registerReceiver(mIntentReceiver, intentFilter);
    }

    @Override
    protected void onPause() {
        super.onPause();
        this.unregisterReceiver(this.mIntentReceiver);
    }
}

Messages.xml 代码:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <Button
        android:id="@+id/btnNew"
        android:layout_width="120dp"
        android:layout_height="wrap_content"
        android:text="New" />

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

</LinearLayout>

这是logcat:

04-16 06:46:48.442: D/dalvikvm(773): GC_CONCURRENT freed 69K, 8% free 2749K/2960K, paused 70ms+12ms, total 122ms
04-16 06:46:48.672: E/ArrayAdapter(773): You must supply a resource ID for a TextView
04-16 06:46:48.672: D/AndroidRuntime(773): Shutting down VM
04-16 06:46:48.672: W/dalvikvm(773): threadid=1: thread exiting with uncaught exception (group=0x40a71930)
04-16 06:46:48.702: E/AndroidRuntime(773): FATAL EXCEPTION: main
04-16 06:46:48.702: E/AndroidRuntime(773): java.lang.IllegalStateException: ArrayAdapter requires the resource ID to be a TextView
04-16 06:46:48.702: E/AndroidRuntime(773):  at android.widget.ArrayAdapter.createViewFromResource(ArrayAdapter.java:386)
04-16 06:46:48.702: E/AndroidRuntime(773):  at android.widget.ArrayAdapter.getView(ArrayAdapter.java:362)
04-16 06:46:48.702: E/AndroidRuntime(773):  at android.widget.AbsListView.obtainView(AbsListView.java:2159)
04-16 06:46:48.702: E/AndroidRuntime(773):  at android.widget.ListView.makeAndAddView(ListView.java:1831)
04-16 06:46:48.702: E/AndroidRuntime(773):  at android.widget.ListView.fillDown(ListView.java:674)
04-16 06:46:48.702: E/AndroidRuntime(773):  at android.widget.ListView.fillFromTop(ListView.java:735)
04-16 06:46:48.702: E/AndroidRuntime(773):  at android.widget.ListView.layoutChildren(ListView.java:1652)
04-16 06:46:48.702: E/AndroidRuntime(773):  at android.widget.AbsListView.onLayout(AbsListView.java:1994)
04-16 06:46:48.702: E/AndroidRuntime(773):  at android.view.View.layout(View.java:14008)
04-16 06:46:48.702: E/AndroidRuntime(773):  at android.view.ViewGroup.layout(ViewGroup.java:4373)
04-16 06:46:48.702: E/AndroidRuntime(773):  at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1663)
04-16 06:46:48.702: E/AndroidRuntime(773):  at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1521)
04-16 06:46:48.702: E/AndroidRuntime(773):  at android.widget.LinearLayout.onLayout(LinearLayout.java:1434)
04-16 06:46:48.702: E/AndroidRuntime(773):  at android.view.View.layout(View.java:14008)
04-16 06:46:48.702: E/AndroidRuntime(773):  at android.view.ViewGroup.layout(ViewGroup.java:4373)
04-16 06:46:48.702: E/AndroidRuntime(773):  at android.widget.FrameLayout.onLayout(FrameLayout.java:448)
04-16 06:46:48.702: E/AndroidRuntime(773):  at android.view.View.layout(View.java:14008)
04-16 06:46:48.702: E/AndroidRuntime(773):  at android.view.ViewGroup.layout(ViewGroup.java:4373)
04-16 06:46:48.702: E/AndroidRuntime(773):  at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1663)
04-16 06:46:48.702: E/AndroidRuntime(773):  at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1521)
04-16 06:46:48.702: E/AndroidRuntime(773):  at android.widget.LinearLayout.onLayout(LinearLayout.java:1434)
04-16 06:46:48.702: E/AndroidRuntime(773):  at android.view.View.layout(View.java:14008)
04-16 06:46:48.702: E/AndroidRuntime(773):  at android.view.ViewGroup.layout(ViewGroup.java:4373)
04-16 06:46:48.702: E/AndroidRuntime(773):  at android.widget.FrameLayout.onLayout(FrameLayout.java:448)
04-16 06:46:48.702: E/AndroidRuntime(773):  at android.view.View.layout(View.java:14008)
04-16 06:46:48.702: E/AndroidRuntime(773):  at android.view.ViewGroup.layout(ViewGroup.java:4373)
04-16 06:46:48.702: E/AndroidRuntime(773):  at android.view.ViewRootImpl.performLayout(ViewRootImpl.java:1892)
04-16 06:46:48.702: E/AndroidRuntime(773):  at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1711)
04-16 06:46:48.702: E/AndroidRuntime(773):  at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:989)
04-16 06:46:48.702: E/AndroidRuntime(773):  at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:4351)
04-16 06:46:48.702: E/AndroidRuntime(773):  at android.view.Choreographer$CallbackRecord.run(Choreographer.java:749)
04-16 06:46:48.702: E/AndroidRuntime(773):  at android.view.Choreographer.doCallbacks(Choreographer.java:562)
04-16 06:46:48.702: E/AndroidRuntime(773):  at android.view.Choreographer.doFrame(Choreographer.java:532)
04-16 06:46:48.702: E/AndroidRuntime(773):  at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:735)
04-16 06:46:48.702: E/AndroidRuntime(773):  at android.os.Handler.handleCallback(Handler.java:725)
04-16 06:46:48.702: E/AndroidRuntime(773):  at android.os.Handler.dispatchMessage(Handler.java:92)
04-16 06:46:48.702: E/AndroidRuntime(773):  at android.os.Looper.loop(Looper.java:137)
04-16 06:46:48.702: E/AndroidRuntime(773):  at android.app.ActivityThread.main(ActivityThread.java:5041)
04-16 06:46:48.702: E/AndroidRuntime(773):  at java.lang.reflect.Method.invokeNative(Native Method)
04-16 06:46:48.702: E/AndroidRuntime(773):  at java.lang.reflect.Method.invoke(Method.java:511)
04-16 06:46:48.702: E/AndroidRuntime(773):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
04-16 06:46:48.702: E/AndroidRuntime(773):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
04-16 06:46:48.702: E/AndroidRuntime(773):  at dalvik.system.NativeStart.main(Native Method)
04-16 06:46:48.702: E/AndroidRuntime(773): Caused by: java.lang.ClassCastException: android.widget.LinearLayout cannot be cast to android.widget.TextView
04-16 06:46:48.702: E/AndroidRuntime(773):  at android.widget.ArrayAdapter.createViewFromResource(ArrayAdapter.java:379)
04-16 06:46:48.702: E/AndroidRuntime(773):  ... 42 more
04-16 06:46:51.462: I/Process(773): Sending signal. PID: 773 SIG: 9

请帮助我为什么它会崩溃。

更新:

新的Logcat:

04-16 13:18:06.314: E/Trace(804): error opening trace file: No such file or directory (2)
04-16 13:18:06.724: D/dalvikvm(804): GC_CONCURRENT freed 69K, 8% free 2773K/2988K, paused 15ms+54ms, total 149ms
04-16 13:18:06.864: D/(804): HostConnection::get() New Host Connection established 0x2a1609e8, tid 804
04-16 13:18:20.764: D/AndroidRuntime(804): Shutting down VM
04-16 13:18:20.764: W/dalvikvm(804): threadid=1: thread exiting with uncaught exception (group=0x40a71930)
04-16 13:18:20.784: E/AndroidRuntime(804): FATAL EXCEPTION: main
04-16 13:18:20.784: E/AndroidRuntime(804): java.lang.ClassCastException: com.example.smsTest.Message cannot be cast to java.lang.String
04-16 13:18:20.784: E/AndroidRuntime(804):  at com.example.smsTest.Messages.onListItemClick(Messages.java:53)
04-16 13:18:20.784: E/AndroidRuntime(804):  at android.app.ListActivity$2.onItemClick(ListActivity.java:319)
04-16 13:18:20.784: E/AndroidRuntime(804):  at android.widget.AdapterView.performItemClick(AdapterView.java:298)
04-16 13:18:20.784: E/AndroidRuntime(804):  at android.widget.AbsListView.performItemClick(AbsListView.java:1100)
04-16 13:18:20.784: E/AndroidRuntime(804):  at android.widget.AbsListView$PerformClick.run(AbsListView.java:2749)
04-16 13:18:20.784: E/AndroidRuntime(804):  at android.widget.AbsListView$1.run(AbsListView.java:3423)
04-16 13:18:20.784: E/AndroidRuntime(804):  at android.os.Handler.handleCallback(Handler.java:725)
04-16 13:18:20.784: E/AndroidRuntime(804):  at android.os.Handler.dispatchMessage(Handler.java:92)
04-16 13:18:20.784: E/AndroidRuntime(804):  at android.os.Looper.loop(Looper.java:137)
04-16 13:18:20.784: E/AndroidRuntime(804):  at android.app.ActivityThread.main(ActivityThread.java:5041)
04-16 13:18:20.784: E/AndroidRuntime(804):  at java.lang.reflect.Method.invokeNative(Native Method)
04-16 13:18:20.784: E/AndroidRuntime(804):  at java.lang.reflect.Method.invoke(Method.java:511)
04-16 13:18:20.784: E/AndroidRuntime(804):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
04-16 13:18:20.784: E/AndroidRuntime(804):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
04-16 13:18:20.784: E/AndroidRuntime(804):  at dalvik.system.NativeStart.main(Native Method)
4

2 回答 2

3

ArrayAdapter 适配器 = new ArrayAdapter(this, R.layout.messages, values);

第二个参数(布局)应该是 TextView。请检查:http: //developer.android.com/reference/android/widget/ArrayAdapter.html#ArrayAdapter%28android.content.Context,%20int,%20java.util.List%3CT%3E%29

包含在实例化视图时使用的 TextView 的布局文件的资源 ID。

于 2013-04-16T06:55:45.903 回答
0

你在给listview提供id时在你的xml文件中遇到了问题。在你的代码中检查这个

<ListView
        android:id="@android:id/list"  //change it to android:id="@android:@+id/list"
        android:layout_width="wrap_content"
        android:layout_height="435dp" />
于 2013-04-16T10:21:45.597 回答