为什么我的程序打不开?代码说没有错误,但是这两个类不会一起打开
Why won't my program open? The code says there are no errors, but these two classes won't open together
这两个 classes 来回发送方法和变量,我的代码说没有错误,但是程序打不开,我不明白为什么
这是第一个class,TopLevelActivity
package com.example.oife;
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 {
TextView mText = (TextView) findViewById(R.id.calendarView);
private ShareActionProvider shareActionProvider;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_oife);
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);
}
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());
}
}
这是第二个class,PostActivity
这个 class 是访问另一个的大部分功能的地方
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;
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 name = (TextView) findViewById(R.id.name);
private TextView day;
TopLevelActivity top = new TopLevelActivity();
@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);
/** Listener for click event of the button */
pPickDate.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
showDialog(DATE_DIALOG_ID);
}
});
/** 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();
}
private void setMainText(){
TextView d = (TextView) findViewById(R.id.description);
mainText.setText(top.getMainText() + "/n/n" + name + "/n" + makeDay().getText() + "/n" + d.getText());
}
private TextView makeDay(){
TextView ToD;
RadioGroup t = (RadioGroup) findViewById(R.id.TOD);
int id = t.getCheckedRadioButtonId();
if(id == -1) {
ToD = null;
}
else{
ToD = (TextView) findViewById(id);
}
TextView wow;
RadioGroup g = (RadioGroup) findViewById(R.id.WoW);
id = g.getCheckedRadioButtonId();
if (id == -1){
//no item selected
}
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";
}
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();
}
};
private void updateDisplay() {
pDisplayDate.setText(
new StringBuilder()
// Month is 0 based so add 1
.append(pMonth + 1).append("/")
.append(pDay).append("/")
.append(pYear).append(" "));
}
private void updateMainDisplay(){
setMainText();
top.setMText(mainText);
}
private 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){
Intent intent = new Intent(this, TopLevelActivity.class);
updateMainDisplay();
startActivity(intent);
}
}
在调用 setContentView
:
之后调用这样的代码
Activity 1:
TextView mText = (TextView) findViewById(R.id.calendarView);
Activity 2:
private TextView name = (TextView) findViewById(R.id.name);
你在两个方面都做错了 Activitys
。您在 Activitys
的成员部分调用 findViewById
,此时 Activity
对布局文件一无所知。
那一定会导致 NullpointerException
。
这两个 classes 来回发送方法和变量,我的代码说没有错误,但是程序打不开,我不明白为什么
这是第一个class,TopLevelActivity
package com.example.oife;
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 {
TextView mText = (TextView) findViewById(R.id.calendarView);
private ShareActionProvider shareActionProvider;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_oife);
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);
}
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());
}
}
这是第二个class,PostActivity
这个 class 是访问另一个的大部分功能的地方
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;
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 name = (TextView) findViewById(R.id.name);
private TextView day;
TopLevelActivity top = new TopLevelActivity();
@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);
/** Listener for click event of the button */
pPickDate.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
showDialog(DATE_DIALOG_ID);
}
});
/** 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();
}
private void setMainText(){
TextView d = (TextView) findViewById(R.id.description);
mainText.setText(top.getMainText() + "/n/n" + name + "/n" + makeDay().getText() + "/n" + d.getText());
}
private TextView makeDay(){
TextView ToD;
RadioGroup t = (RadioGroup) findViewById(R.id.TOD);
int id = t.getCheckedRadioButtonId();
if(id == -1) {
ToD = null;
}
else{
ToD = (TextView) findViewById(id);
}
TextView wow;
RadioGroup g = (RadioGroup) findViewById(R.id.WoW);
id = g.getCheckedRadioButtonId();
if (id == -1){
//no item selected
}
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";
}
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();
}
};
private void updateDisplay() {
pDisplayDate.setText(
new StringBuilder()
// Month is 0 based so add 1
.append(pMonth + 1).append("/")
.append(pDay).append("/")
.append(pYear).append(" "));
}
private void updateMainDisplay(){
setMainText();
top.setMText(mainText);
}
private 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){
Intent intent = new Intent(this, TopLevelActivity.class);
updateMainDisplay();
startActivity(intent);
}
}
在调用 setContentView
:
Activity 1:
TextView mText = (TextView) findViewById(R.id.calendarView);
Activity 2:
private TextView name = (TextView) findViewById(R.id.name);
你在两个方面都做错了 Activitys
。您在 Activitys
的成员部分调用 findViewById
,此时 Activity
对布局文件一无所知。
那一定会导致 NullpointerException
。