0

所以基本上我有 ListView 和第二个活动,其中有两个 EditText 保存数据(有点像笔记应用程序)。ListView 显示它们已创建,但是当我单击 ListView 中的一个项目时,它会显示一个没有数据的空白第二个活动。我尝试了多种使用捆绑包来解决此问题的方法,但到目前为止它仍然没有显示任何内容。

这是我目前的第一个活动(ListView 活动)...

    List.setOnItemClickListener(new OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
            Intent launchActivity = new Intent(MainActivityPlanes.this, MainActivityPaper.class);

            public static final String keyname = "android.application.project.planes.MainActivityPlanes.Extra_keyname1";
            Bundle b = new Bundle();
            String keyname = title1.getText().toString();
            String key = text1.getText().toString(); 
            b.putString("Extra__keyname", keyname);
            b.putString("Extra__key", key);
            launchActivity.putExtras(b);

            startActivity(launchActivity);

这是我的第二个活动(包含两个 TextView 的活动)中捆绑包的第二段代码......

    Intent launchActvity = getIntent();

    Bundle b = launchActvity.getExtras();

    String keyname = launchActvity.getStringExtra(MainActivityPlanes.Extra_keyname);
    String key = launchActvity.getStringExtra(MainActivityPlanes.Extra_key);

    TextView title1 = (TextView)findViewById(R.id.editText3);
    TextView text1 = (TextView)findViewById(R.id.editText1);

    title1.setText("keyname");
    text1.setText("key");

关于如何解决这个问题的任何想法?

第一个活动

 package android.application.project.planes;

import java.util.ArrayList;

import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
 import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;

 public class MainActivityPlanes extends Activity {

Button Add;
ListView List;

@Override
    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    this.requestWindowFeature(Window.FEATURE_NO_TITLE);
    setContentView(R.layout.activity_main_activity_planes);

    Add = (Button) findViewById(R.id.button1);
    List = (ListView) findViewById(R.id.listView1);

    Add.setOnClickListener(new OnClickListener () {

        public void onClick (View v) {
            Intent intent = new Intent (v.getContext(), MainActivityPaper.class);
            startActivityForResult(intent, 0);
        }

    });

    List.setOnItemClickListener(new OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
            Intent launchActivity = new Intent(MainActivityPlanes.this, MainActivityPaper.class);

             Bundle b = new Bundle();
             String keyname = title1.getText().toString();
             String key = text1.getText().toString(); 
             b.putString("Extra__keyname", keyname);
             b.putString("Extra__key", key);
             launchActivity.putExtras(b);

             startActivity(launchActivity);

        }

    });

    MainActivityChampagne info = new MainActivityChampagne(this);       
    info.open();        
    ArrayList<String> data = info.getData();
    info.close();
    ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1, data);
    List.setAdapter(arrayAdapter);

}

}

第二次活动

package android.application.project.planes;

import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.Button;
import android.widget.TextView;

public class MainActivityPaper extends Activity implements OnClickListener {

TextView text1, title1;
Button save1, update1;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    this.requestWindowFeature(Window.FEATURE_NO_TITLE);
    setContentView(R.layout.activity_main_activity_paper);

    text1 = (TextView) findViewById(R.id.editText1);
    title1 = (TextView) findViewById(R.id.editText3);
    save1 = (Button) findViewById(R.id.button3);
    update1 = (Button) findViewById(R.id.button5);

    save1.setOnClickListener(this);
    update1.setOnClickListener(this);

}

@Override
public void onClick(View arg0) {
    // TODO Auto-generated method stub

    switch (arg0.getId()) {
    case R.id.button3:

        String titles = title1.getText().toString();
        String notes = text1.getText().toString();

        MainActivityChampagne entry = new MainActivityChampagne(MainActivityPaper.this);
        entry.open();
        entry.createEntry(titles, notes);
        entry.close();

        break;

    case R.id.button5:

        String thetitles = title1.getText().toString();
        String thenotes = text1.getText().toString();

        MainActivityChampagne up = new MainActivityChampagne(MainActivityPaper.this);
        up.open();
        up.updateEntry(thetitles, thenotes);
        up.close();

        break;

    }

    Intent launchActvity = getIntent();

    Bundle b = launchActvity.getExtras();

    String keyname = launchActvity.getStringExtra("keyname");
    String key = launchActvity.getStringExtra("key");

    TextView title1 = (TextView)findViewById(R.id.editText3);
    TextView text1 = (TextView)findViewById(R.id.editText1);

    title1.setText("keyname");
    text1.setText("key");

}

}

我的第三个活动(SQL 数据库活动)...

 package android.application.project.planes;

 import java.util.ArrayList;

 import android.app.Activity;
 import android.content.ContentValues;
 import android.content.Context;
 import android.database.Cursor;
 import android.database.sqlite.SQLiteDatabase;
 import android.database.sqlite.SQLiteOpenHelper;

public class MainActivityChampagne extends Activity {

public static final String KEY_NAME = "title";
public static final String KEY_NOTES = "lastcall";

private static final String DATABASE_NAME = "Champagnedb";
private static final String DATABASE_TABLE = "champagneTable";
private static final int DATABASE_VERSION = 1;

private DbHelper ourHelper;
private final Context ourContext;
private SQLiteDatabase ourDatabase;

private static class DbHelper extends SQLiteOpenHelper{

    public DbHelper(Context context){
        super(context, DATABASE_NAME, null, DATABASE_VERSION);
        // TODO Auto-generated constructor stub

    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        // TODO Auto-generated method stub\
        db.execSQL("CREATE TABLE " + DATABASE_TABLE + " (" +
        KEY_NAME + " TEXT NOT NULL);"
        );

    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        // TODO Auto-generated method stub
        db.execSQL("DROP TABLE IF EXIST " + DATABASE_TABLE);
        onCreate(db);

    }

}

public MainActivityChampagne(Context c){
    ourContext = c;

}

public MainActivityChampagne open(){
    ourHelper = new DbHelper(ourContext);
    ourDatabase = ourHelper.getWritableDatabase();
    return this;

}

public void close(){
    ourHelper.close();

}

public long createEntry(String titles, String notes) {
    // TODO Auto-generated method stub
    ContentValues cv = new ContentValues();
    cv.put(KEY_NAME, titles);
    return ourDatabase.insert(DATABASE_TABLE, null, cv);

}

public ArrayList<String> getData() {
    //TODO Auto-generated method stub
    String[] columns = new String[] { KEY_NAME };
    Cursor c = ourDatabase.query(DATABASE_TABLE, columns, null, null, null, null, null);
    String result = "";

    int iNotes = c.getColumnIndex(KEY_NAME);

    ArrayList<String> string_array = new ArrayList<String>();
    for (c.moveToFirst(); !c.isAfterLast(); c.moveToNext())

    {

        result = c.getString (iNotes) + "\n";
        string_array.add(result);

        }

    return string_array;

}

public void updateEntry(String thetitles, String thenotes) {
    // TODO Auto-generated method stub
    ContentValues cvUpdate = new ContentValues();
    cvUpdate.put(KEY_NAME, thetitles);
    cvUpdate.put(KEY_NOTES, thenotes);
    ourDatabase.update(DATABASE_TABLE, cvUpdate, KEY_NAME + "=" + thetitles + KEY_NOTES + "=" + thenotes, null);

}

 }

日志猫

06-23 15:42:19.710: E/StrictMode(638):  at android.app.LoadedApk$ServiceDispatcher.<init>(LoadedApk.java:969)
06-23 15:42:19.710: E/StrictMode(638):  at android.app.LoadedApk.getServiceDispatcher(LoadedApk.java:863)
06-23 15:42:19.710: E/StrictMode(638):  at android.app.ContextImpl.bindService(ContextImpl.java:1418)
06-23 15:42:19.710: E/StrictMode(638):  at android.app.ContextImpl.bindService(ContextImpl.java:1407)
06-23 15:42:19.710: E/StrictMode(638):  at android.content.ContextWrapper.bindService(ContextWrapper.java:473)
06-23 15:42:19.710: E/StrictMode(638):  at com.android.emailcommon.service.ServiceProxy.setTask(ServiceProxy.java:157)
06-23 15:42:19.710: E/StrictMode(638):  at com.android.emailcommon.service.ServiceProxy.setTask(ServiceProxy.java:145)
06-23 15:42:19.710: E/StrictMode(638):  at com.android.emailcommon.service.AccountServiceProxy.getDeviceId(AccountServiceProxy.java:116)
06-23 15:42:19.710: E/StrictMode(638):  at com.android.exchange.ExchangeService.getDeviceId(ExchangeService.java:1249)
06-23 15:42:19.710: E/StrictMode(638):  at com.android.exchange.ExchangeService$7.run(ExchangeService.java:1856)
06-23 15:42:19.710: E/StrictMode(638):  at com.android.emailcommon.utility.Utility$2.doInBackground(Utility.java:551)
06-23 15:42:19.710: E/StrictMode(638):  at com.android.emailcommon.utility.Utility$2.doInBackground(Utility.java:549)
06-23 15:42:19.710: E/StrictMode(638):  at android.os.AsyncTask$2.call(AsyncTask.java:287)
06-23 15:42:19.710: E/StrictMode(638):  at java.util.concurrent.FutureTask.run(FutureTask.java:234)
06-23 15:42:19.710: E/StrictMode(638):  at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080)
06-23 15:42:19.710: E/StrictMode(638):  at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573)
06-23 15:42:19.710: E/StrictMode(638):  at java.lang.Thread.run(Thread.java:856)
06-23 15:42:19.710: W/ActivityManager(284): Unbind failed: could not find connection for android.os.BinderProxy@40d605b0
06-23 15:42:19.729: E/ActivityThread(638): Service com.android.exchange.ExchangeService has leaked ServiceConnection com.android.emailcommon.service.ServiceProxy$ProxyConnection@40d11750 that was originally bound here
06-23 15:42:19.729: E/ActivityThread(638): android.app.ServiceConnectionLeaked: Service com.android.exchange.ExchangeService has leaked ServiceConnection com.android.emailcommon.service.ServiceProxy$ProxyConnection@40d11750 that was originally bound here
06-23 15:42:19.729: E/ActivityThread(638):  at android.app.LoadedApk$ServiceDispatcher.<init>(LoadedApk.java:969)
06-23 15:42:19.729: E/ActivityThread(638):  at android.app.LoadedApk.getServiceDispatcher(LoadedApk.java:863)
06-23 15:42:19.729: E/ActivityThread(638):  at android.app.ContextImpl.bindService(ContextImpl.java:1418)
06-23 15:42:19.729: E/ActivityThread(638):  at android.app.ContextImpl.bindService(ContextImpl.java:1407)
06-23 15:42:19.729: E/ActivityThread(638):  at android.content.ContextWrapper.bindService(ContextWrapper.java:473)
06-23 15:42:19.729: E/ActivityThread(638):  at com.android.emailcommon.service.ServiceProxy.setTask(ServiceProxy.java:157)
06-23 15:42:19.729: E/ActivityThread(638):  at com.android.emailcommon.service.ServiceProxy.setTask(ServiceProxy.java:145)
06-23 15:42:19.729: E/ActivityThread(638):  at com.android.emailcommon.service.ServiceProxy.test(ServiceProxy.java:191)
06-23 15:42:19.729: E/ActivityThread(638):  at com.android.exchange.ExchangeService$7.run(ExchangeService.java:1850)
06-23 15:42:19.729: E/ActivityThread(638):  at com.android.emailcommon.utility.Utility$2.doInBackground(Utility.java:551)
06-23 15:42:19.729: E/ActivityThread(638):  at com.android.emailcommon.utility.Utility$2.doInBackground(Utility.java:549)
06-23 15:42:19.729: E/ActivityThread(638):  at android.os.AsyncTask$2.call(AsyncTask.java:287)
06-23 15:42:19.729: E/ActivityThread(638):  at java.util.concurrent.FutureTask.run(FutureTask.java:234)
06-23 15:42:19.729: E/ActivityThread(638):  at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080)
06-23 15:42:19.729: E/ActivityThread(638):  at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573)
06-23 15:42:19.729: E/ActivityThread(638):  at java.lang.Thread.run(Thread.java:856)
06-23 15:42:19.858: E/StrictMode(638): null
06-23 15:42:19.858: E/StrictMode(638): android.app.ServiceConnectionLeaked: Service com.android.exchange.ExchangeService has leaked ServiceConnection com.android.emailcommon.service.ServiceProxy$ProxyConnection@40d11750 that was originally bound here
06-23 15:42:19.858: E/StrictMode(638):  at android.app.LoadedApk$ServiceDispatcher.<init>(LoadedApk.java:969)
06-23 15:42:19.858: E/StrictMode(638):  at android.app.LoadedApk.getServiceDispatcher(LoadedApk.java:863)
06-23 15:42:19.858: E/StrictMode(638):  at android.app.ContextImpl.bindService(ContextImpl.java:1418)
06-23 15:42:19.858: E/StrictMode(638):  at android.app.ContextImpl.bindService(ContextImpl.java:1407)
06-23 15:42:19.858: E/StrictMode(638):  at android.content.ContextWrapper.bindService(ContextWrapper.java:473)
06-23 15:42:19.858: E/StrictMode(638):  at com.android.emailcommon.service.ServiceProxy.setTask(ServiceProxy.java:157)
06-23 15:42:19.858: E/StrictMode(638):  at com.android.emailcommon.service.ServiceProxy.setTask(ServiceProxy.java:145)
06-23 15:42:19.858: E/StrictMode(638):  at com.android.emailcommon.service.ServiceProxy.test(ServiceProxy.java:191)
06-23 15:42:19.858: E/StrictMode(638):  at com.android.exchange.ExchangeService$7.run(ExchangeService.java:1850)
06-23 15:42:19.858: E/StrictMode(638):  at com.android.emailcommon.utility.Utility$2.doInBackground(Utility.java:551)
06-23 15:42:19.858: E/StrictMode(638):  at com.android.emailcommon.utility.Utility$2.doInBackground(Utility.java:549)
06-23 15:42:19.858: E/StrictMode(638):  at android.os.AsyncTask$2.call(AsyncTask.java:287)
06-23 15:42:19.858: E/StrictMode(638):  at java.util.concurrent.FutureTask.run(FutureTask.java:234)
06-23 15:42:19.858: E/StrictMode(638):  at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080)
06-23 15:42:19.858: E/StrictMode(638):  at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573)
06-23 15:42:19.858: E/StrictMode(638):  at java.lang.Thread.run(Thread.java:856)
06-23 15:42:19.889: W/ActivityManager(284): Unbind failed: could not find connection for android.os.BinderProxy@40db5f30
06-23 15:42:19.889: D/dalvikvm(638): GC_CONCURRENT freed 466K, 20% free 2493K/3080K, paused 75ms+84ms, total 272ms
4

3 回答 3

0

我相信你的问题在这里:

b.putString("Extra__keyname", keyname);
b.putString("Extra__key", key);

和这里:

String keyname = launchActivity.getStringExtra("keyname");
String key = launchActivity.getStringExtra("key");

您正在尝试使用第一个 Activity 中未使用的 String 检索 StringExtra(您"Extra__keyname" and "Extra__key"在第二个 Activity 中使用过,您必须使用这些特定字符串,此外,作为一种良好做法,额外的“key”字符串应该是公共常量您的第一个 Activity,然后在第二个 Activity 中使用相同的常量获取 getStringExtra,如下所示:

 public static final String EXTRA_KEY_NAME = "com.example.yourclass.EXTRA_KEY_NAME";

这进入你的第一个活动,然后你把额外的像这样:

 b.putString(EXTRA_KEY_NAME, keyname);

然后在您的第二个活动中,您将获得这样的 Extra:

 launchActivity.getStringExtra(FirstActivity.EXTRA_KEY_NAME);

这是编程术语的更好方法,并由谷歌推荐,对不起我的英语,问候。

于 2013-06-23T16:01:52.703 回答
0

将您的第一个活动代码更改为

package android.application.project.planes;

import java.util.ArrayList;

import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
 import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;

 public class MainActivityPlanes extends Activity {

Button Add;
ListView List;

@Override
    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    this.requestWindowFeature(Window.FEATURE_NO_TITLE);
    setContentView(R.layout.activity_main_activity_planes);

    Add = (Button) findViewById(R.id.button1);
    List = (ListView) findViewById(R.id.listView1);

    Add.setOnClickListener(new OnClickListener () {

        public void onClick (View v) {
            Intent intent = new Intent (v.getContext(), MainActivityPaper.class);
            startActivityForResult(intent, 0);
        }

    });

    List.setOnItemClickListener(new OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
            Intent launchActivity = new Intent(MainActivityPlanes.this, MainActivityPaper.class);

             Bundle b = new Bundle();
             String keyname =                   ((TextView)arg1.findViewById(android.R.id.text1)).getText().toString();
             //String key = text1.getText().toString(); 
             b.putString("Extra__keyname", keyname);
            //b.putString("Extra__key", key);
             launchActivity.putExtras(b);

             startActivity(launchActivity);

        }

    });

    MainActivityChampagne info = new MainActivityChampagne(this);       
    info.open();        
    ArrayList<String> data = info.getData();
    info.close();
    ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1, data);
    List.setAdapter(arrayAdapter);

}

}

将您的第二个活动课程更改为

package android.application.project.planes;

import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.Button;
import android.widget.TextView;

public class MainActivityPaper extends Activity implements OnClickListener {

TextView text1, title1;
Button save1, update1;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    this.requestWindowFeature(Window.FEATURE_NO_TITLE);
    setContentView(R.layout.activity_main_activity_paper);

    text1 = (TextView) findViewById(R.id.editText1);
    title1 = (TextView) findViewById(R.id.editText3);
    save1 = (Button) findViewById(R.id.button3);
    update1 = (Button) findViewById(R.id.button5);


    Intent launchActvity = getIntent();

    Bundle b = launchActvity.getExtras();

    String keyname = b.getString("Extra__keyname");
   //this string same as b.putString("Extra__keyname", keyname);
   // String key = b.getString("Extra__key");

    TextView title1 = (TextView)findViewById(R.id.editText3);
    TextView text1 = (TextView)findViewById(R.id.editText1);

    title1.setText(keyname);
    //text1.setText(key);

    save1.setOnClickListener(this);
    update1.setOnClickListener(this);

}

@Override
public void onClick(View arg0) {
    // TODO Auto-generated method stub

    switch (arg0.getId()) {
    case R.id.button3:

        String titles = title1.getText().toString();
        String notes = text1.getText().toString();

        MainActivityChampagne entry = new MainActivityChampagne(MainActivityPaper.this);
        entry.open();
        entry.createEntry(titles, notes);
        entry.close();

        break;

    case R.id.button5:

        String thetitles = title1.getText().toString();
        String thenotes = text1.getText().toString();

        MainActivityChampagne up = new MainActivityChampagne(MainActivityPaper.this);
        up.open();
        up.updateEntry(thetitles, thenotes);
        up.close();

        break;

    }
}

}
于 2013-06-23T17:31:12.053 回答
0

发现应该是:

Bundle b = launchActivity.getExtras();

String keyname = b.getString("keyname");
String key = b.getString("key");

你可能需要删除 Extra__

于 2013-06-23T04:23:22.557 回答