我是编程和应用程序的新手我每次从第一个活动切换到第二个应用程序崩溃时都会构建。 这是我的代码: 主:
public class MainActivity extends Activity {
private static final String MOOD_AMOUNT = "MOOD_AMOUNT";
public String eaten;
public String inputData;
public boolean visual = false;
public boolean auditory = false;
public boolean selfInjury = false;
SeekBar rateMoodSeekBar;
private double moodAmount;
EditText moodAmountET;
CheckBox visualCheckBox;
CheckBox auditoryCheckBox;
CheckBox selfInjuryCheckBox;
RadioGroup eatenRadioGroup;
RadioButton eatenPoorRadioButton;
RadioButton eatenOKRadioButton;
RadioButton eatenWellRadioButton;
RadioButton eatenNoneRadioButton;
Button submitButton;
Button viewFilesButton;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if(savedInstanceState == null){
// Just started
moodAmount = 50;
} else {
// App is being restored
moodAmount = savedInstanceState.getDouble(MOOD_AMOUNT);
}
// Initialize the EditTexts
moodAmountET = (EditText) findViewById(R.id.rateMoodEditText);
addChangeListenersToTextEdit();
visualCheckBox= (CheckBox) findViewById(R.id.visualCheckBox);
auditoryCheckBox= (CheckBox) findViewById(R.id.auditoryCheckBox);
selfInjuryCheckBox= (CheckBox) findViewById(R.id.selfInjuryCheckBox);
setUpCheckBoxes();
eatenRadioGroup= (RadioGroup) findViewById(R.id.eatenRadioGroup);
eatenOKRadioButton= (RadioButton) findViewById(R.id.eatenOkRadioButton);
eatenPoorRadioButton= (RadioButton) findViewById(R.id.eatenPoorRadioButton);
eatenWellRadioButton= (RadioButton) findViewById(R.id.eatenWellRadioButton);
eatenNoneRadioButton= (RadioButton) findViewById(R.id.eatenNoneRadioButton);
addChangeListenersToRadios();
rateMoodSeekBar = (SeekBar) findViewById(R.id.rateMoodSeekBar);
rateMoodSeekBar.setOnSeekBarChangeListener(rateMoodSeekBarListener);
submitButton = (Button) findViewById(R.id.submitButton);
viewFilesButton = (Button) findViewById(R.id.viewFilesButton);
setButtonOnClickListeners();
}
private void addChangeListenersToTextEdit() {
moodAmountET.addTextChangedListener(new TextWatcher(){
@Override
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
}
@Override
public void onTextChanged(CharSequence s, int start, int before,
int count) {
}
@Override
public void afterTextChanged(Editable s) {
rateMoodSeekBar.setProgress(Integer.valueOf(moodAmountET.getText().toString()));
}
});
}
private void addChangeListenersToRadios() {
eatenRadioGroup.setOnCheckedChangeListener(new OnCheckedChangeListener(){
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
eaten = (eatenNoneRadioButton.isChecked())?"None":null;
eaten = (eatenPoorRadioButton.isChecked())?"Poor":null;
eaten = (eatenOKRadioButton.isChecked())?"OK":null;
eaten = (eatenWellRadioButton.isChecked())?"Well":null;
}
});
}
private void setUpCheckBoxes() {
visualCheckBox.setOnCheckedChangeListener(new CheckBox.OnCheckedChangeListener(){
@Override
public void onCheckedChanged(CompoundButton buttonView,
boolean isChecked) {
visual = isChecked;
}
});
auditoryCheckBox.setOnCheckedChangeListener(new CheckBox.OnCheckedChangeListener(){
@Override
public void onCheckedChanged(CompoundButton buttonView,
boolean isChecked) {
auditory = isChecked;
}
});
selfInjuryCheckBox.setOnCheckedChangeListener(new CheckBox.OnCheckedChangeListener(){
@Override
public void onCheckedChanged(CompoundButton buttonView,
boolean isChecked) {
selfInjury = isChecked;
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, 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();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
private OnSeekBarChangeListener rateMoodSeekBarListener = new OnSeekBarChangeListener(){
@Override
public void onProgressChanged(SeekBar arg0, int arg1, boolean arg2) {
// Get the value set on the SeekBar
moodAmount = (rateMoodSeekBar.getProgress());
// Set tipAmountET with the value from the SeekBar
moodAmountET.setText(String.format("%.00f", moodAmount));
// Update all the other EditTexts
//updateMood();
}
@Override
public void onStartTrackingTouch(SeekBar arg0) {
}
@Override
public void onStopTrackingTouch(SeekBar arg0) {
}
};
@SuppressWarnings("unused")
private void updateMood(){
// Get tip amount
moodAmountET.setText(String.format("%.02f", moodAmount));
}
protected void onSaveInstanceState(Bundle outState){
super.onSaveInstanceState(outState);
outState.putDouble(MOOD_AMOUNT, moodAmount);
}
private void setButtonOnClickListeners() {
submitButton.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View v) {
getInputData();
writeSubmitInternal(inputData);
//dialog box
AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
builder.setTitle(R.string.submitted_alert);
builder.setPositiveButton("OK", null);
builder.setMessage(R.string.submitted_alert);
AlertDialog theAlertDialog = builder.create();
theAlertDialog.show();
}
});
}
protected void getInputData() {
String tempDataCompile;
tempDataCompile = moodAmountET.getText().toString();
tempDataCompile += " " + eaten;
tempDataCompile += " " + String.valueOf(visual) + " " + String.valueOf(auditory) + " " + String.valueOf(selfInjury); //checkboxes
inputData = tempDataCompile;
}
public void writeSubmitInternal(String data){
String fileName;
Calendar cal = Calendar.getInstance();
String theYear = String.valueOf(cal.get(Calendar.YEAR));
String theMonth = String.valueOf(cal.get(Calendar.MONTH));
String theDay = String.valueOf(cal.get(Calendar.DAY_OF_MONTH));
String theHour = String.valueOf(cal.get(Calendar.HOUR_OF_DAY));
fileName = theYear+"_"+theMonth+ "_"+theDay+"_"+theHour+".txt";
FileOutputStream outputStream;
try {
outputStream = openFileOutput(fileName, Context.MODE_PRIVATE);
outputStream.write(data.getBytes());
outputStream.close();
} catch (Exception e) {
e.printStackTrace();
}
}
public void nextScreen(View view){
Intent intent = new Intent(this, ListFilesActivity.class);
startActivity(intent);
}
}
第二项活动
public class ListFilesActivity extends Activity {
Button openFileButton;
File[] fileList;
String[] fileNames;
Spinner spinner;
private ArrayAdapter<String> myAdapter1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_list_files);
getFiles();
//------------------
spinner = (Spinner)findViewById(R.id.fileSpinner);
myAdapter1 = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, fileNames);
myAdapter1.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(myAdapter1);
openFileButton = (Button) findViewById(R.id.openFileButton);
openFileButton.setOnClickListener(openFileButtonListener);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.list_files, 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();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
public void getFiles(){
File path = this.getFilesDir();
fileList = path.listFiles();
fileNames[0] = "None";
if(fileList[0]!=null){
for(int i = 0; i<fileList.length;i++){
fileNames[i]=fileList[i].getName();
}
}
}
public void deleteSingleFile(String fileName){
int index=-1;
for(int i = 0; i<fileNames.length;i++){
index = fileNames[i].equals(fileName)?i:-1;
}
if (index==-1){
displayAlert("File Not Found");
}
else{
fileList[index].delete();
}
}
private void displayAlert(String message) {
AlertDialog.Builder builder = new AlertDialog.Builder(ListFilesActivity.this);
builder.setTitle("Alert");
builder.setPositiveButton("OK", null);
builder.setMessage(message);
AlertDialog theAlertDialog = builder.create();
theAlertDialog.show();
}
public void deleteAllFiles(){
getFiles();
for(int i = 0; i<fileList.length;i++){
fileList[i].delete();
}
}
public OnClickListener openFileButtonListener = new OnClickListener(){
@Override
public void onClick(View v) {
//FileInputStream fis = openFileInput(FILENAME, Context.MODE_PRIVATE);
}
};
}
和数据日志:
11-26 21:08:08.877: D/AndroidRuntime(20621): Shutting down VM
11-26 21:08:08.877: W/dalvikvm(20621): threadid=1: thread exiting with uncaught exception (group=0x41622d40)
11-26 21:08:08.882: E/AndroidRuntime(20621): FATAL EXCEPTION: main
11-26 21:08:08.882: E/AndroidRuntime(20621): Process: tumblr.worldofmyown.mood_app_android, PID: 20621
11-26 21:08:08.882: E/AndroidRuntime(20621): java.lang.RuntimeException: Unable to start activity ComponentInfo{tumblr.worldofmyown.mood_app_android/tumblr.worldofmyown.mood_app_android.ListFilesActi vity}: java.lang.NullPointerException
11-26 21:08:08.882: E/AndroidRuntime(20621): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2198)
11-26 21:08:08.882: E/AndroidRuntime(20621): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2257)
11-26 21:08:08.882: E/AndroidRuntime(20621): at android.app.ActivityThread.access$800(ActivityThread.java:139)
11-26 21:08:08.882: E/AndroidRuntime(20621): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1210)
11-26 21:08:08.882: E/AndroidRuntime(20621): at android.os.Handler.dispatchMessage(Handler.java:102)
11-26 21:08:08.882: E/AndroidRuntime(20621): at android.os.Looper.loop(Looper.java:136)
11-26 21:08:08.882: E/AndroidRuntime(20621): at android.app.ActivityThread.main(ActivityThread.java:5086)
11-26 21:08:08.882: E/AndroidRuntime(20621): at java.lang.reflect.Method.invokeNative(Native Method)
11-26 21:08:08.882: E/AndroidRuntime(20621): at java.lang.reflect.Method.invoke(Method.java:515)
11-26 21:08:08.882: E/AndroidRuntime(20621): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
11-26 21:08:08.882: E/AndroidRuntime(20621): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
11-26 21:08:08.882: E/AndroidRuntime(20621): at dalvik.system.NativeStart.main(Native Method)
11-26 21:08:08.882: E/AndroidRuntime(20621): Caused by: java.lang.NullPointerException
11-26 21:08:08.882: E/AndroidRuntime(20621): at tumblr.worldofmyown.mood_app_android.ListFilesActivity.getFiles(ListFilesActivity.java:73)
11-26 21:08:08.882: E/AndroidRuntime(20621): at tumblr.worldofmyown.mood_app_android.ListFilesActivity.onCreate(ListFilesActivity.java:33)
11-26 21:08:08.882: E/AndroidRuntime(20621): at android.app.Activity.performCreate(Activity.java:5248)
11-26 21:08:08.882: E/AndroidRuntime(20621): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1110)
11-26 21:08:08.882: E/AndroidRuntime(20621): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2162)
11-26 21:08:08.882: E/AndroidRuntime(20621): ... 11 more
11-26 21:08:11.644: I/Process(20621): Sending signal. PID: 20621 SIG: 9