我正在尝试从NoteActivity发送一个ArrayList到MainActivity。
ArrayList<String> data = new ArrayList<String>();
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_note_pad);
final FloatingActionButton button =
(FloatingActionButton)findViewById(R.id.save_button);
button.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View v)
{
SaveData();
}
});
}
public void SaveData()
{
FileOutputStream outputStream;
try
{
EditText editText = findViewById(R.id.textView3);
final String str = editText.getText().toString();
String content = findViewById(R.id.textView).toString();
Log.d("Test","inside try block");
outputStream = openFileOutput(str, Context.MODE_PRIVATE);
outputStream.write(content.getBytes());
Toast.makeText(getApplicationContext(),"Note
saved",Toast.LENGTH_SHORT).show();
Log.d("Test","save done");
final String str1 = editText.getText().toString();
data.add(str1);
SendData();
}
catch (FileNotFoundException e)
{
e.printStackTrace();
}
catch (IOException e)
{
e.printStackTrace();
}
}
public void SendData()
{
EditText editText = findViewById(R.id.textView3);
Log.d("Check","str:"+data);
Intent intent = new Intent(NotePad.this,MainActivity.class);
intent.putExtra(EXTRA_MESSAGE, data);
startActivity(intent);
}
}
My Main Activity
public class MainActivity extends AppCompatActivity
{
private RecyclerView recyclerView;
private RecyclerView.Adapter mAdapter;
private RecyclerView.LayoutManager layoutManager;
ArrayList<String> notes = new ArrayList<String>();
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Intent intent = getIntent();
notes = intent.getStringArrayListExtra(NotePad.EXTRA_MESSAGE);
Log.d("Check","title added: "+notes);
final FloatingActionButton button =
(FloatingActionButton)findViewById(R.id.add_notebutton);
button.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View v)
{
Intent intent = new Intent(MainActivity.this,NotePad.class);
startActivity(intent);
}
});
recyclerView = (RecyclerView)findViewById(R.id.recycle_list);
layoutManager = new LinearLayoutManager(this);
recyclerView.setLayoutManager(layoutManager);
mAdapter = new MyAdapter(notes);
recyclerView.setAdapter(mAdapter);
}
}
my Adapter code
public class MyAdapter extends RecyclerView.Adapter<MyAdapter.MyViewHolder>
{
ArrayList<String> Notes_list = new ArrayList<String>();
public MyAdapter(ArrayList<String> notes)
{
Notes_list = notes;
}
@NonNull
@Override
public MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int
viewType)
{
LayoutInflater inflater = LayoutInflater.from(parent.getContext());
View view = inflater.inflate(R.layout.list_items,parent,false);
return new MyViewHolder(view);
}
@Override
public void onBindViewHolder(@NonNull MyViewHolder holder, int position)
{
holder.mTextView.setText(Notes_list.get(position));
}
@Override
public int getItemCount()
{
return Notes_list.size();
}
public class MyViewHolder extends RecyclerView.ViewHolder
{
TextView mTextView;
public MyViewHolder(@NonNull View itemView)
{
super(itemView);
mTextView = (TextView) itemView.findViewById(R.id.view_holder);
}
}
}
我在笔记(arrayList)中未收到任何数据。当我运行代码
该应用程序立即崩溃,并出现空指针异常。
错误:java.lang.NullPointerException:尝试调用虚拟方法'int java.util.ArrayList.size()'上的空对象引用 com.example.quicknote.MyAdapter.getItemCount(MyAdapter.java:39)
答案 0 :(得分:1)
尝试一下:-
PATH=/usr/bin
答案 1 :(得分:1)
您正在尝试传递ArrayList,并使用getStringArrayListExtra方法从意图中读取数据。您必须像这样放置数据:
Engine engine = Engine.newInstance(EngineOptions.newBuilder(RenderingMode.OFF_SCREEN)
.licenseKey("1BNDHFSC1FTA7A76VKWRRM8IRYEAL48AT20PIS26JQO0CK3H6Y0P58QPVB084Y1K4IU98T").build());
编辑:检查意图是否包含键:
intent.putStringArrayListExtra(EXTRA_MESSAGE, data);
注意:如果这只是一个游乐场,那就可以了。但是如果你试图 建立东西,那么你的架构是错误的。这不是一个好主意 在两个活动之间每次传递相同的数组。您可以使用 startActivityForResult方法并在onActivityResult方法上获得新注释 MainActivity。
答案 2 :(得分:0)
android文档中给出的将arraylist或任何自定义对象从一个活动发送到另一个活动的最佳方法,以尝试在android中对此进行轻量级处理
阅读有关它的完整概述https://developer.android.com/reference/android/os/Parcelable
希望对您有帮助