1

我做了一個簡單的Android應用程序與2 activity..in第1 3 editTexts和2按鈕「插入」和「取消」在那裏,在第二個活動它的listView.I想要當按下ctivity1的插入按鈕時,從第一個acitivity輸入的edittext中的3個值應該轉到第二個activity並顯示在一個簡單的listView中...我試過下面的代碼,但它不工作... Emulator顯示「不幸的應用程序停止」 ..EditTexts和ListView練習在Android不工作

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

} 

小號econdactivity.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> 
+0

請發佈您的logcat跟蹤 – Pragnani 2013-05-10 09:23:10

+0

我已經把Logcat跟蹤...先生 – jigar 2013-05-10 09:40:20

回答

0

您還沒有指定您的按鈕ID。您剛剛聲明瞭按鈕bt.you必須獲取second.xml中指定的按鈕的引用。

在錯誤中顯示nullpointer異常在第33行。 這是您編寫按鈕單擊代碼的位置。 編輯您的代碼並提供參考按鈕bt如下。

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

也在第一個活動編輯像這樣。

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

我試過你的代碼與按鈕refernce.Its工作正常。

+0

@jigar:在bt.setOnClickListener(新的OnClickListener(){)之前添加Button bt =(Button)findViewById(R.id.button1){它能正常工作 – anjaly 2013-05-10 10:39:08

+0

我已經發布了在我的答案中。檢查一次,在發佈答案之前詢問他 – Pragnani 2013-05-10 10:43:28

0

編輯:

在列表視圖中的XML改變ID來android:id="@android:id/list"並在你的類得到它這樣

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

當您使用ListActivity會發生這種情況,因爲ListActivity應有的列表視圖與編號android.R.id.list點擊此處查看參考List Activity


您的按鈕Button btnull因爲你沒有獲得其在onCreate()

嘗試參考,讓您的按鈕這樣

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

我想這是你的問題,請發表logcat的跟蹤,這樣我將發佈根據其回答

+0

我已經cahnges作爲你sugession ..但仍然不工作...!sri..please幫助我....它的我的實際測試爲工作..!它可以拯救我的lyf ..! – jigar 2013-05-10 09:32:03

+0

@jigar在這裏發佈您的logcat跟蹤...以便我可以幫助..編輯您的問題並在您的logcat窗口中發佈紅線 – Pragnani 2013-05-10 09:33:31

+0

好的...先生...等等...我張貼你.. – jigar 2013-05-10 09:35:13

0

你SecondActivity被擴展ListActivity,所以你不需要的setContentView和ListView控件可以參考使用getListView()

+0

'getListView()'不會工作,因爲xml中的listview應該有id'android:id/list'' – Pragnani 2013-05-10 09:46:10

+0

兄弟..我已經刪除了「listactivity」並且放置了「activity」仍然不工作...... :( – jigar 2013-05-10 09:48:02

+0

你可以把新的logacat放在這裏嗎? – ArturSkowronski 2013-05-10 09:52:02