从哪里开始.. 我是Android的新手,并尝试为Android创建一个小应用程序,显示时间表(5天8小时),这些将显示为40个方格的表格。用户可以通过选择Day(来自微调器),Time(微调器),添加注释(最多15个字符)并选择背景颜色来添加事件。所有这些都必须添加到字符串数组并发送到主要活动。从那里通过从该字符串数组中获取日期和时间槽值,将使用来自相同字符串数组的文本和背景颜色更新适当的正方形。我已经尝试解决这个问题大约四天了,但是我无法理解这个...这里有一些代码: 主要活动:
package com.tt;
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.Button;
import android.widget.TextView;
public class MainActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button settings = (Button)findViewById(R.id.settings);
Intent a = getIntent();
String[] add = a.getStringArrayExtra("strings");
setContentView(R.layout.activity_main);
TextView detail = (TextView)findViewById(R.id.m1);
//detail.setText(notes);
settings.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
Intent i = new Intent(v.getContext(),Settings.class);
startActivityForResult(i,0);
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
}
添加活动:
package com.tt;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioGroup;
import android.widget.RadioGroup.OnCheckedChangeListener;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;
import android.app.Activity;
import android.content.Intent;
public class Add extends Activity implements OnClickListener, OnCheckedChangeListener {
String[] DOW;
String[] time;
String clr;
EditText nts;
boolean click = true;
Button save;
RadioGroup days;
RadioGroup times;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.add);
initialize();
DOW = getResources().getStringArray(R.array.DOW);
Spinner s1 =(Spinner)findViewById(R.id.spinner1);
ArrayAdapter<String>adapter = new ArrayAdapter<String>(this,android.R.layout.simple_spinner_dropdown_item,DOW);
s1.setAdapter(adapter);
s1.setOnItemSelectedListener(new OnItemSelectedListener() {
public void onItemSelected(AdapterView<?>arg0,View arg1, int arg2, long arg3) {
int index = arg0.getSelectedItemPosition();
Toast.makeText(getBaseContext(), "Chosen Day of the Week is "+ DOW[index], Toast.LENGTH_SHORT).show();
}
@Override
public void onNothingSelected(AdapterView<?>arg0){
// nothing
}
});
time = getResources().getStringArray(R.array.time);
Spinner s2 = (Spinner)findViewById(R.id.spinner2);
ArrayAdapter<String> adapter2 = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_dropdown_item,time);
s2.setAdapter(adapter2);
s2.setOnItemSelectedListener(new OnItemSelectedListener() {
public void onItemSelected(AdapterView<?>arg0,View arg1, int arg2, long arg3) {
int index = arg0.getSelectedItemPosition();
Toast.makeText(getBaseContext(), "Time is set to "+ time[index], Toast.LENGTH_SHORT).show();
}
@Override
public void onNothingSelected(AdapterView<?> arg0) {
// nothing
}
});
}
private void initialize() {
save = (Button)findViewById(R.id.save);
save.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
String notes = nts.getText().toString();
String day = DOW.toString();
String tim = time.toString();
//String color = clr.
Bundle b = new Bundle();
/*bag.putString("key1", day);
//bag.putString("key2",tim);
//bag.putString("key3", notes);
//bag.putString("key4", color);*/
/*String[] added = new String[4];
added[0]=day;
added[1]=tim;
added[2]=color;
added[3]=notes;*/
//b.putStringArray("key",added);
String[] added = new String[] {day, tim, notes};
//b.putStringArray("key", new String[]{day, tim, notes});
Intent a = new Intent(Add.this, MainActivity.class);
a.putExtra("strings",added);
startActivity(a);
}
});
nts = (EditText) findViewById(R.id.notes);
//days.setOnCheckedChangeListener((OnCheckedChangeListener) this);
//times.setOnCheckedChangeListener((OnCheckedChangeListener) this);
}
@Override
public void onClick(View arg0) {
// nothing
}
@Override
public void onCheckedChanged(RadioGroup arg0, int arg1) {
switch(arg1) {
case R.id.blue:
clr = "blue";
break;
case R.id.green:
clr = "green";
break;
case R.id.yellow:
clr = "yellow";
break;
case R.id.red:
clr = "red";
break;
}
}
}
更新
是的...我已经将字符串数组减少到只有一个字符串......这是假设发送数据的代码:
private void initialize()
{
save = (Button)findViewById(R.id.save);
nts = (EditText) findViewById(R.id.notes);
}
public void onClick(View arg0)
{
String notes = nts.getText().toString();
//String day = DOW.toString();
//String tim = time.toString();
Bundle b = new Bundle();
b.putString("AddedNote", notes);
Intent a = new Intent(Add.this, MainActivity.class);
a.putExtras(b);
startActivity(a);
/*String[] added = new String[] {notes};
a.putExtra("strings",added);
*/
};
和“接收者”:
public class MainActivity extends Activity
{
String gotNotes;
TextView notes;
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initialize();
Button settings = (Button)findViewById(R.id.settings);
Bundle gotPackage = getIntent().getExtras();
gotNotes = gotPackage.getString("AddedNote");
if (gotNotes != "")
{
notes.setText(gotNotes);
}
else{}
settings.setOnClickListener(new OnClickListener()
{
public void onClick(View v)
{
Intent i = new Intent(v.getContext(),Settings.class);
startActivityForResult(i,0);
}
});
}
private void initialize()
{
// TODO Auto-generated method stub
notes = (TextView)findViewById(R.id.m1);
}
@Override
public boolean onCreateOptionsMenu(Menu menu)
{
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
}
我得到的结果是由java.lang.nullpointerexception引起的:
gotNotes = gotPackage.getString("AddedNote");
“接收者”活动的..
答案 0 :(得分:0)
这取决于你的活动android:launchMode,我怀疑你有singleTask或类似的东西。在这种情况下,你应该覆盖onNewIntent,你应该在那里接收你的字符串数组的意图。
替代解决方案是将数据存储在共享首选项中,如果需要,可以将其序列化为JSON对象。
protected void onNewIntent(Intent intent) {
String[] add = a.getStringArrayExtra("strings");
}