Android Can';无法从ListActivity启动活动

Android Can';无法从ListActivity启动活动,android,android-activity,listactivity,Android,Android Activity,Listactivity,我不知道这个代码有什么问题。我的应用程序在运行时崩溃 代码如下: 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.Int

我不知道这个代码有什么问题。我的应用程序在运行时崩溃

代码如下:

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;

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

    private BroadcastReceiver mIntentReceiver;
    ListView listview;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        btnNew = (Button) findViewById(R.id.btnNew);

        super.onCreate(savedInstanceState);
        setContentView(R.layout.messages);

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

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

        ArrayAdapter<Message> adapter = new ArrayAdapter<Message>(this,
                android.R.layout.simple_list_item_1, 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 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);
    }
}

必须在使用
LayoutInflater
或调用
setContentView(R.layout.messages)对布局进行充气后找到视图

修改

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

super.onCreate(savedInstanceState);
setContentView(R.layout.messages);


只有代码的答案通常不好。你能补充一个解释让OP知道为什么他们的错误吗?我的错。我想我可以把它放在任何地方。Thanks@jaypabs原因是您的
视图
存在于
版面
中,因此如果您没有使用
充气机或调用
setContentView()
版面
充气,它将返回
null
btnNew = (Button) findViewById(R.id.btnNew);

super.onCreate(savedInstanceState);
setContentView(R.layout.messages);
super.onCreate(savedInstanceState);
setContentView(R.layout.messages);

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