1

我制作了一个带有 2 个活动的简单 android 应用程序。在第 1 个 3 editTexts 和 2 个按钮“插入”和“取消”中,在第二个活动中它是一个 listView。我想要的是当按下 ctivity1 的插入按钮时的值从第一个活动输入 3 个编辑文本应该转到第二个活动并显示在一个简单的列表视图中...我尝试了以下代码,但它不起作用...模拟器显示“不幸的是应用程序停止”..

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

    }

秒活动.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>

日志猫

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

清单.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>
4

3 回答 3

0

编辑:

在您的 Xml 中将 listview 的 id 更改为android:id="@android:id/list"并在您的 Class 中像这样

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

当您使用 ListActivity 时会发生这种情况,因为 ListActivity 应该有带有 id 的 listviewandroid.R.id.list检查此处以获取参考List Activity


你的按钮Button btnull因为你没有得到它的参考onCreate()

试着让你的按钮像这样

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

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

于 2013-05-10T09:24:06.740 回答
0

您的 SecondActivity 是 ListActivity 的扩展,因此您不需要 setContentView 并且 ListView 可以参考使用 getListView()

于 2013-05-10T09:42:43.223 回答
0

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

在错误中,它显示空指针异常位于第 33 行。这是您编写按钮单击代码的地方。编辑您的代码并参考下面的按钮 bt。

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

同样在这样的第一个活动编辑中。

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

我用按钮引用尝试了你的代码。它工作正常。

于 2013-05-10T10:29:58.670 回答