我正在尝试创建一个"新闻应用程序"使用Android Studio。我的主要活动是ListView
和floating button
。在我的第二项活动中,我获得了two TextViews
,two EditViews
和one Button
。我想做的是以下内容:
首先,我按下主要活动中的floating Button
。然后会弹出第二个活动,然后我在two EditText's
上写下,然后按button
。之后我应该回到主要活动那里,应该在ListView中添加一个新项目。新项目应填写第二个活动的EditTexts中的文本。
这是我的主要活动:
package news;
import android.content.Intent;
import android.support.design.widget.FloatingActionButton;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//Get subject value from addpost Activity
final String new_value = getIntent().getStringExtra("newSubject");
String [] new_subject = new String []
{new_value};
FloatingActionButton add_post_button = findViewById(R.id.post_btn);
final ListView post_view = findViewById(R.id.news_feed);
//Create Adapter to add the new subject to listview
if (new_subject != null) {
ArrayAdapter newslist_adapter = new ArrayAdapter(
MainActivity.this,
android.R.layout.simple_expandable_list_item_1, new_subject);
post_view.setAdapter(newslist_adapter);
}
add_post_button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent(MainActivity.this, addpost_activity.class);
MainActivity.this.startActivity(intent);
}
});
}
}
这是第二个活动(addpost_activity):
package news;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.content.Intent;
import android.widget.EditText;
import android.widget.ListView;
import java.util.ArrayList;
public class addpost_activity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_addpost_activity);
Button post_button = findViewById(R.id.post_btn);
final EditText subject_text = findViewById(R.id.subject_edit);
post_button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if(subject_text.getText() != null) {
Intent intent = new Intent(getApplicationContext(), MainActivity.class);
intent.putExtra("newSubject", subject_text.getText().toString());
startActivity(intent);
}
}
});
}
}
我的问题是,每次启动时应用程序都会崩溃。我也从日志中收到此消息:
java.lang.NullPointerException:尝试调用虚方法' java.lang.String java.lang.Object.toString()'在空对象引用上
解决: 感谢Munir,问题解决了。
以下是我的活动
MainActivity:
package news;
import android.app.Activity;
import android.content.Intent;
import android.support.design.widget.FloatingActionButton;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import java.util.ArrayList;
public class MainActivity extends AppCompatActivity {
ArrayAdapter newslist_adapter;
ArrayList<String> new_subject = new ArrayList<>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
FloatingActionButton add_post_button = findViewById(R.id.post_btn);
//Create Adapter to add the new subject to listview
add_post_button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent(MainActivity.this, addpost_activity.class);
startActivityForResult(intent, 1);
}
});
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
final ListView post_view = findViewById(R.id.news_feed);
if (requestCode == 1) {
if(resultCode == Activity.RESULT_OK){
String new_value = data.getStringExtra("newSubject");
new_subject.add(new_value);
newslist_adapter = new ArrayAdapter(
MainActivity.this,
android.R.layout.simple_expandable_list_item_1, new_subject);
post_view.setAdapter(newslist_adapter);
}
}
}
}
这是我的第二个活动(addpost_activity):
package news;
import android.app.Activity;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.content.Intent;
import android.widget.EditText;
import android.widget.ListView;
import java.util.ArrayList;
public class addpost_activity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_addpost_activity);
Button post_button = findViewById(R.id.post_btn);
final EditText subject_text = findViewById(R.id.subject_edit);
post_button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if(subject_text.getText() != null) {
Intent intent = new Intent(getApplicationContext(), MainActivity.class);
intent.putExtra("newSubject", subject_text.getText().toString());
setResult(Activity.RESULT_OK, intent);
finish();
}
}
});
}
}
答案 0 :(得分:3)
为此,我建议使用Fragments
代替两个Activities
。然后MainActivity
会保留您的所有数据。
请参阅Fragments guide。
答案 1 :(得分:1)
从MainActivity
使用addpost_activity
方法调用startActivityForResult()
而不是startActivity();
在addpost_activity
中设置要返回FirstActivity
post_button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if(subject_text.getText() != null) {
Intent intent = new Intent(getApplicationContext(), MainActivity.class);
intent.putExtra("newSubject", subject_text.getText().toString());
setResult(Activity.RESULT_OK,intent );
finish();
}
}
});
在MainActivity
通过覆盖onActivityResult()
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == 1) {
if(resultCode == Activity.RESULT_OK){
String result=data.getStringExtra("newSubject");
//Add this value to your adapter and call notifyDataSetChanged();
}
}
}
答案 2 :(得分:1)
如果你想在列表中保留旧条目,请使用String []的ArrayList,如下所示更改你的代码。
主要活动
将此变量声明为全局
ArrayAdapter newslist_adapter;
ArrayList<String> new_subject = new ArrayList<>();
设置适配器
newslist_adapter = new ArrayAdapter(
MainActivity.this,
android.R.layout.simple_expandable_list_item_1, new_subject);
post_view.setAdapter(newslist_adapter);
将startActivity更改为startActivityForResult
add_post_button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent(MainActivity.this, addpost_activity.class);
startActivityForResult(intent,100);
}
});
在onActivity中覆盖此方法
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == 100) {
String result=data.getStringExtra("newSubject");
new_subject.add(result)
//Add this value to your adapter and call newslist_adapter.notifyDataSetChanged();
}
}
在你的addpost_activity中设置你想要返回的数据
post_button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if(subject_text.getText() != null) {
Intent intent = new Intent(getApplicationContext(), MainActivity.class);
intent.putExtra("newSubject", subject_text.getText().toString());
setResult(100,intent );
finish();
}
}
});
答案 3 :(得分:0)
在MainActivity.java中,您应该覆盖名为“onActivityRecsult”的函数,当您从第二个活动返回到第一个活动时,将调用此方法。