gpt4 book ai didi

java - 我的程序不会从另一个类更新 TextView ,它每次都会退出,我不知道为什么?

转载 作者:行者123 更新时间:2023-12-03 17:45:39 24 4
gpt4 key购买 nike

顶级 Activity

此 Activity 是从我的另一个类收到显示更新的 Activity ,发布 Activity

这两个程序的目标是来回发送显示,从一个屏幕到另一个屏幕,并从 Activity 后类更新主要 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());
}

}

现在这里是发布 Activity

此类具有更新显示功能,应更新其他类中的 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)

最佳答案

这是非常错误的:

top = new TopLevelActivity();

您无法创建 Activity 实例来使用某个值对其进行更新,您必须将此值传递给现有实例。为此,使用 startActivityForResult 创建新 Activity 并在 TopLevelActivy.onActivityResult 中接收它。更多信息请阅读: http://developer.android.com/training/basics/intents/result.html

关于java - 我的程序不会从另一个类更新 TextView ,它每次都会退出,我不知道为什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34595933/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com