android中的EditTexts和ListView Practice无法正常工作

时间:2013-05-10 09:18:47

标签: android android-intent android-listview android-arrayadapter

我做了一个简单的Android应用程序有2个活动..在第3个editTexts和2个按钮“插入”和“取消”都在那里,在第二个活动中它是一个listView.I想要的是当ctivity1的插入按钮是按下第1个活动中3个edittexts中输入的值应该转到第2个活动并显示在一个简单的列表中...我试过以下代码但它不起作用...模拟器显示“不幸的应用停止了”..

mainActivity.java

package com.example.listdemo;

import java.util.ArrayList;

import android.R.id;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;

public class MainActivity extends Activity {
Button b1,b2;
EditText e1,e2,e3;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        final EditText e1=(EditText)findViewById(R.id.editText1);
        final EditText e2=(EditText)findViewById(R.id.editText2);
        final EditText e3=(EditText)findViewById(R.id.editText3);


      //  final ArrayList<String> noteList = new ArrayList<String>();
        //final ArrayAdapter<String> aa;
        b1=(Button)findViewById(R.id.button1);
        b2=(Button)findViewById(R.id.button2);
    b1.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View arg0) {
            // TODO Auto-generated method stub
            String s1 = e1.getText().toString();
            String s2 = e1.getText().toString();
            String s3 = e1.getText().toString();

            Bundle b=new Bundle();
            b.putStringArray("key", new String[]{s1, s2,s3});
            Intent i=new Intent(getApplicationContext(), SecondActivity.class);
            i.putExtras(b);
            startActivity(i);

        }
    });
    b2.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
        Intent i = new Intent(MainActivity.this,SecondActivity.class);
        startActivity(i);
        }
    });
    }

}

secondactivity.java

   package com.example.listdemo;

import android.os.Bundle;
import android.app.Activity;
import android.app.ListActivity;
import android.content.Intent;
import android.sax.StartElementListener;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.Toast;

public class SecondActivity extends Activity {
    ListView ls;
    Button bt;
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_second);

              ls=(ListView)findViewById(R.id.list1);

              Bundle b=this.getIntent().getExtras();
              String[] array=b.getStringArray("key");

              ls.setAdapter(new ArrayAdapter(this, android.R.layout.simple_list_item_1,array));



            bt.setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View arg0) {
                    // TODO Auto-generated method stub
                Intent i =new Intent(SecondActivity.this,MainActivity.class);
                startActivity(i);
                }
            });
        }

    }

seconActivity.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <ListView
        android:id="@+id/list1"
        android:layout_width="match_parent"
        android:layout_height="306dp" >
    </ListView>

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Back"
        android:layout_marginTop="20dp"
        android:layout_gravity="center" />

</LinearLayout>

logcat的

05-10 15:41:50.279: W/KeyCharacterMap(3629): No keyboard for id 0
05-10 15:41:50.279: W/KeyCharacterMap(3629): Using default keymap: /system/usr/keychars/qwerty.kcm.bin
05-10 15:41:58.657: D/AndroidRuntime(3629): Shutting down VM
05-10 15:41:58.667: W/dalvikvm(3629): threadid=1: thread exiting with uncaught exception (group=0x40015560)
05-10 15:41:58.688: E/AndroidRuntime(3629): FATAL EXCEPTION: main
05-10 15:41:58.688: E/AndroidRuntime(3629): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.listdemo/com.example.listdemo.SecondActivity}: java.lang.NullPointerException
05-10 15:41:58.688: E/AndroidRuntime(3629):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1647)
05-10 15:41:58.688: E/AndroidRuntime(3629):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
05-10 15:41:58.688: E/AndroidRuntime(3629):     at android.app.ActivityThread.access$1500(ActivityThread.java:117)
05-10 15:41:58.688: E/AndroidRuntime(3629):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
05-10 15:41:58.688: E/AndroidRuntime(3629):     at android.os.Handler.dispatchMessage(Handler.java:99)
05-10 15:41:58.688: E/AndroidRuntime(3629):     at android.os.Looper.loop(Looper.java:123)
05-10 15:41:58.688: E/AndroidRuntime(3629):     at android.app.ActivityThread.main(ActivityThread.java:3683)
05-10 15:41:58.688: E/AndroidRuntime(3629):     at java.lang.reflect.Method.invokeNative(Native Method)
05-10 15:41:58.688: E/AndroidRuntime(3629):     at java.lang.reflect.Method.invoke(Method.java:507)
05-10 15:41:58.688: E/AndroidRuntime(3629):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
05-10 15:41:58.688: E/AndroidRuntime(3629):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
05-10 15:41:58.688: E/AndroidRuntime(3629):     at dalvik.system.NativeStart.main(Native Method)
05-10 15:41:58.688: E/AndroidRuntime(3629): Caused by: java.lang.NullPointerException
05-10 15:41:58.688: E/AndroidRuntime(3629):     at com.example.listdemo.SecondActivity.onCreate(SecondActivity.java:33)
05-10 15:41:58.688: E/AndroidRuntime(3629):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
05-10 15:41:58.688: E/AndroidRuntime(3629):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
05-10 15:41:58.688: E/AndroidRuntime(3629):     ... 11 more
05-10 15:42:00.727: I/Process(3629): Sending signal. PID: 3629 SIG: 9

的manifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.listdemo"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="17" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.example.listdemo.MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity
            android:name="com.example.listdemo.SecondActivity"
            android:label="@string/title_activity_second" >
        </activity>
    </application>
</manifest>

3 个答案:

答案 0 :(得分:0)

修改

在您的列表视图的Xml更改ID android:id="@android:id/list"和您的类中,将其设为这样

lv=(ListView)findViewById(android.R.id.list);

当您使用ListActivity时会发生这种情况,因为ListActivity应该包含ID为android.R.id.list的列表视图。点击此处查看参考List Activity


您的按钮Button btnull,因为您未在onCreate()

中获取其引用

尝试按下这样的按钮

bt=(Button)findViewById(R.id.yourButtonid);

我想这是你的问题,请发布logcat跟踪,以便我根据它发布答案

答案 1 :(得分:0)

您的SecondActivity扩展了ListActivity,因此您不需要setContentView,而ListView可以引用getListView()

答案 2 :(得分:0)

您尚未指定按钮ID。您刚刚声明了按钮bt.you必须获得在second.xml中指定的按钮的引用。

在错误中显示nullpointer Exception在第33行。 这是您编写按钮单击代码的地方。 编辑您的代码并参考按钮bt,如下所示。

按钮bt =(按钮)findViewById(R.id.button Id)。

同样在第一次活动编辑中。

        String s1 = e1.getText().toString();
        String s2 = e2.getText().toString();
        String s3 = e3.getText().toString();

我尝试使用按钮引用代码。它工作正常。