此活动是从我的其他类Post Activity
收到显示更新的活动 package com.example.oife;
import android.content.Context;
import android.media.Image;
import android.app.Activity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.ShareActionProvider;
import android.widget.TextView;
import android.content.Intent;
import android.view.View;
import java.text.SimpleDateFormat;
public class TopLevelActivity extends Activity {
public TextView mText;
//Context context;
// private ShareActionProvider shareActionProvider;
/*public TopLevelActivity(Context context){
this.context = context;
}*/
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_oife);
mText = (TextView) findViewById(R.id.calendarView);
long date = System.currentTimeMillis();
TextView displayDate = (TextView) findViewById(R.id.displayDate);
SimpleDateFormat sdf = new SimpleDateFormat("MMM dd, yyyy");
String dateString = sdf.format(date);
displayDate.setText(dateString);
}
/* @Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_oife, menu);
MenuItem menuItem = menu.findItem(R.id.action_settings);
shareActionProvider = (ShareActionProvider) menuItem.getActionProvider();
//setIntent("This is example text");
return super.onCreateOptionsMenu(menu);
}*/
/*private void setIntent(String text) {
Intent intent = new Intent(Intent.ACTION_SEND);
intent.setType("text/plain");
intent.putExtra(Intent.EXTRA_TEXT, text);
shareActionProvider.setShareIntent(intent);
}*/
/* @Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
switch (item.getItemId()) {
case R.id.action_settings
return true;
default:
return super.onOptionsItemSelected(item);
}
}
*/
//call onPostClick() method when the button is clicked
public void onPostClick (View view) {
Intent intent = new Intent(this, PostActivity.class);
startActivity(intent);
}
//call onHomeClick() method when the button is clicked
public void onHomeClick (View view){
Intent intent = new Intent(this,TopLevelActivity.class);
startActivity(intent);
}
//call onMoreClick() method when the button is clicked
public void onMoreClick(View view){
Intent intent = new Intent(this, MoreActivity.class);
startActivity(intent);
}
public TextView getMainText(){
return mText;
}
public void setMText(TextView t){
mText.setText(t.getText());
}
}
此类具有更新显示功能,该功能应更新其他类
中的textview日历package com.example.oife;
import java.util.Calendar;
import android.app.Activity;
import android.app.DatePickerDialog;
import android.app.Dialog;
import android.os.Bundle;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.RadioGroup;
import android.widget.Toast;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.TextView;
import android.content.Intent;
import android.view.View;
import android.content.Context;
public class PostActivity extends Activity {
private TextView pDisplayDate;
private Button pPickDate;
private int pYear;
private int pMonth;
private int pDay;
private TextView MAINText;
static final int DATE_DIALOG_ID = 0;
private TextView day;
private TextView xyz;
TopLevelActivity top;
TextView d ;
RadioGroup t;
RadioGroup g;
protected TopLevelActivity context;
public PostActivity(Context context){
this.context = (TopLevelActivity) context;
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_post);
/** Capture our View elements */
pDisplayDate = (TextView) findViewById(R.id.displayDate);
pPickDate = (Button) findViewById(R.id.pickDate);
xyz = (TextView) findViewById(R.id.name);
top = new TopLevelActivity();
/** Listener for click event of the button */
pPickDate.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
showDialog(DATE_DIALOG_ID);
}
});
t = (RadioGroup) findViewById(R.id.TOD);
d = (TextView) findViewById(R.id.description);
g = (RadioGroup) findViewById(R.id.WoW);
MAINText = new TextView(this);
day = new TextView(this);
/** Get the current date */
final Calendar cal = Calendar.getInstance();
pYear = cal.get(Calendar.YEAR);
pMonth = cal.get(Calendar.MONTH);
pDay = cal.get(Calendar.DAY_OF_MONTH);
/** Display the current date in the TextView */
// updateDisplay();
}
public void setMainText(){
day.setText(makeDay().getText());
this.MAINText.setText(top.getMainText() + "/n/n" + xyz + "/n" + day + "/n" + d.getText());
}
private TextView makeDay(){
TextView ToD;
int id = t.getCheckedRadioButtonId();
if(id == -1) {
ToD = null;
}
else{
ToD = (TextView) findViewById(id);
}
/*TextView wow;
id = g.getCheckedRadioButtonId();
if (id == -1){
wow = null;
}
else{
wow = (TextView) findViewById(id);
}
*/
TextView d = (TextView) findViewById(R.id.displayDate);
String s = String.valueOf(d);
String j;
if (ToD.getText().equals("Morning")){
j = "9:00";
}
else if(ToD.getText().equals("Midday")){
j = "12:00";
}
else if (ToD.getText().equals("Evening")){
j = "5:00";
}
else {
j = "10:00";
}
/*String l;
if (wow.getText().equals("Weekend")){
l="Sunday";
}
else {
l="Wednesday";
}*/
TextView time = new TextView(this);
time.setText( j + " " + s );
return time;
}
private DatePickerDialog.OnDateSetListener pDateSetListener =
new DatePickerDialog.OnDateSetListener() {
public void onDateSet(DatePicker view, int year,
int monthOfYear, int dayOfMonth) {
pYear = year;
pMonth = monthOfYear;
pDay = dayOfMonth;
updateDisplay();
displayToast();
}
};
public void updateDisplay() {
pDisplayDate.setText(
new StringBuilder()
// Month is 0 based so add 1
.append(pMonth + 1).append("/")
.append(pDay).append("/")
.append(pYear).append(" "));
}
public void updateMainDisplay(){
setMainText();
top.setMText(MAINText);
}
/*public void updateTV(final String str1){
context.runOnUiThread(new Runnable() {
@Override
public void run() {
context.getMainText().setText(str1);
}
});
}*/
public void displayToast() {
Toast.makeText(this, new StringBuilder().append("Date choosen is ").append(pDisplayDate.getText()), Toast.LENGTH_SHORT).show();}
/** Create a new dialog for date picker */
@Override
protected Dialog onCreateDialog(int id) {
switch (id) {
case DATE_DIALOG_ID:
return new DatePickerDialog(this,
pDateSetListener,
pYear, pMonth, pDay);
}
return null;
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_post, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
public void onCreateClick(View view){
updateMainDisplay();
Intent intent = new Intent(this, TopLevelActivity.class);
startActivity(intent);
}
}
--------- beginning of crash
01-04 11:56:16.368 2057-2057/com.example.oife E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.example.oife, PID: 2057
java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.oife/com.example.oife.PostActivity}: java.lang.InstantiationException: java.lang.Class<com.example.oife.PostActivity> has no zero argument constructor
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2327)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
at android.app.ActivityThread.-wrap11(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
Caused by: java.lang.InstantiationException: java.lang.Class<com.example.oife.PostActivity> has no zero argument constructor
at java.lang.Class.newInstance(Native Method)
at android.app.Instrumentation.newActivity(Instrumentation.java:1067)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2317)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
at android.app.ActivityThread.-wrap11(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
答案 0 :(得分:1)
这是非常错误的:
top = new TopLevelActivity();
您无法创建活动实例以使用某个值更新它,您必须将此值传递给现有实例。为此,使用startActivityForResult创建新活动,并在TopLevelActivy.onActivityResult中接收它。有关详细信息,请参阅:http://developer.android.com/training/basics/intents/result.html