我是编程新手,遇到一个我自己无法解决的问题。 我想要做的是在MainActivity中向ListView填充/添加一些东西,并将其放入SECOND Activity中的EditText中。每次我单击活动2上的按钮时,我的应用程序都会停止工作。我不知道我做错了什么,所以非常感谢帮助。 我会发布我的代码:
MainActivity:
public class MainActivity extends AppCompatActivity {
EditText et;
ListView lv;
ArrayList<String> arrayList;
ArrayAdapter<String> adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
et = (EditText) findViewById(R.id.et);
lv = (ListView) findViewById(R.id.lv);
arrayList = new ArrayList<String>();
adapter = new ArrayAdapter<String>(MainActivity.this, android.R.layout.simple_expandable_list_item_1, arrayList);
lv.setAdapter(adapter);
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent1 = new Intent(MainActivity.this , Activity_2.class);
startActivityForResult(intent1, 1);//Throws me into Act2
}
});
}
public void onActivityResult(int requestCode, int resultCode, Intent intentback){
super.onActivityResult(requestCode, resultCode, intentback);
if ( requestCode == 1){
if(resultCode == RESULT_OK){
String resultback = intentback.getStringExtra("nameEvent");
arrayList.add(intentback.getStringExtra("nameEvent"));
adapter.notifyDataSetChanged();
}
}
}
}
想把我扔进Activity_2,输入一个名字:
public class Activity_2 extends AppCompatActivity {
EditText etact;
Button addbtn;
String eventname;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_2_layout);
addbtn = (Button) findViewById(R.id.addbtn);
etact = (EditText) findViewById(R.id.etact);
if ( etact.getText().toString()!= ""){
eventname = etact.getText().toString();
}
addbtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (eventname == ""){
Toast.makeText(Activity_2.this,
"Make Input first", Toast.LENGTH_LONG).show();
}
else {
Intent intentback = new Intent(Activity_2.this, MainActivity.class);
intentback.putExtra("nameEvent", eventname);
setResult(RESULT_OK);
finish();
}
}
});
}
}
编辑:Logcat
致命的例外:主程序:com.example.robert.againagainagaian, PID:3857 java.lang.RuntimeException:传递结果失败 ResultInfo {who = null,request = 1,result = -1,data = null}到activity {com.example.robert.againagainagaian / com.example.robert.againagainagaian.MainActivity}: java.lang.NullPointerException:尝试调用虚方法 “java.lang.String中 android.content.Intent.getStringExtra(java.lang.String)'为null 对象参考 android.app.ActivityThread.deliverResults(ActivityThread.java:4053)at android.app.ActivityThread.handleSendResult(ActivityThread.java:4096) 在android.app.ActivityThread.-wrap20(ActivityThread.java)处 android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1516) 在android.os.Handler.dispatchMessage(Handler.java:102)at android.os.Looper.loop(Looper.java:154)at android.app.ActivityThread.main(ActivityThread.java:6077)at java.lang.reflect.Method.invoke(Native Method)at com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:866) 在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:756)引起 by:java.lang.NullPointerException:尝试调用虚方法 “java.lang.String中 android.content.Intent.getStringExtra(java.lang.String)'为null 对象参考 com.example.robert.againagainagaian.MainActivity.onActivityResult(MainActivity.java:55) 在android.app.Activity.dispatchActivityResult(Activity.java:6915)at android.app.ActivityThread.deliverResults(ActivityThread.java:4049)at android.app.ActivityThread.handleSendResult(ActivityThread.java:4096) 在android.app.ActivityThread.-wrap20(ActivityThread.java)处 android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1516) 在android.os.Handler.dispatchMessage(Handler.java:102)at android.os.Looper.loop(Looper.java:154)at android.app.ActivityThread.main(ActivityThread.java:6077)at java.lang.reflect.Method.invoke(Native Method)at com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:866) 在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:756)
答案 0 :(得分:0)
从intent onActivityResult
public void onActivityResult(int requestCode, int resultCode, Intent intentback){
super.onActivityResult(requestCode, resultCode, intentback);
if ( requestCode == 1){
if(resultCode == RESULT_OK && intentback.getData()!=null){
String resultback = intentback.getStringExtra("nameEvent");
arrayList.add(intentback.getStringExtra("nameEvent"));
adapter.notifyDataSetChanged();
}
}
}
在SecondActivity上设置结果像这样
if (!("").equals(etact.getText().toString())){
eventname = etact.getText().toString();
}
addbtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (("").equals(eventname)){
Toast.makeText(Activity_2.this,
"Make Input first", Toast.LENGTH_LONG).show();
}
else {
Intent intentback = new Intent();
intentback.putExtra("nameEvent", eventname);
setResult(RESULT_OK, intent);
finish();
}