Eclipse尝试运行一个方法从一个类到另一个类时,它崩溃了我的应用程序

时间:2018-03-09 09:52:01

标签: java android

我在Eclipse中有2个屏幕。 MainActivityUpdateScreen。方法writeToFileMainActivity屏幕中完美运行。但是,当我从UpdateScreen调用该方法时,它会崩溃应用程序。我已经尝试了几天来找出原因。 (阅读很多或文章)。我无法做对。

MainActivity.java:

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;

import android.os.Bundle;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends Activity {
// variables for write to file
    public static final String[] FILENAME = {"iBuyer.txt","sbuyer.txt","abuyer.txt"};
    static int txtindex = 0; // use this to get the correct  txt file to 

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //setup widgets for use in app
        final EditText idBuyer1 = (EditText)findViewById(R.id.idBuyerTxt);
        Button submit = (Button)findViewById(R.id.submitButton);
        Button update = (Button)findViewById(R.id.updateButton);

        //Code for Submit Button
        submit.setOnClickListener(new OnClickListener(){
        public void onClick(View v){

          //write to file for idbuyer
            String textToSave = idBuyer1.getText().toString();
                try {
                    txtindex = 0;
                     writeToFile(textToSave);
                } catch (FileNotFoundException e1) {
                    e1.printStackTrace();
                }               
        }//code ends for Submit Button
        });

        //Update button code. This will open the update screen
        update.setOnClickListener(new OnClickListener(){
            public void onClick(View v){
                startActivity(new Intent(MainActivity.this, UpdateScreen.class));
            }
        });
    // Write to file method   
   public void writeToFile(String data) throws FileNotFoundException {

        OutputStreamWriter osw = new     OutputStreamWriter(openFileOutput(FILENAME[txtindex],
                                                    Context.MODE_PRIVATE));
        BufferedWriter bw = new BufferedWriter(osw);
        try {
            bw.write(data);
        } catch (IOException e) {
            e.printStackTrace();
            Toast.makeText(MainActivity.this, 
                    "Couldn't write to file", Toast.LENGTH_LONG).show();
        }
        try {
            bw.newLine();
        } catch (IOException e) {
            e.printStackTrace();
            Toast.makeText(MainActivity.this, 
                    "Couldn't write to file", Toast.LENGTH_LONG).show();
        }
        try {
            bw.close();
        } catch (IOException e1) {
            e1.printStackTrace();
            Toast.makeText(MainActivity.this, 
                    "Couldn't write to file", Toast.LENGTH_LONG).show();
        }
        try {
            osw.close();
        } catch (IOException e) {
            e.printStackTrace();
            Toast.makeText(MainActivity.this, 
                    "Couldn't write to file", Toast.LENGTH_LONG).show();

        }

    }


    @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;
    }

}

这是更新屏幕:

import java.io.BufferedWriter;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.OutputStreamWriter;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

public class UpdateScreen extends Activity {
    @Override
    protected void onCreate(Bundle savedInstances){
        super.onCreate(savedInstances);
        setContentView(R.layout.updatescreen);
        final EditText updateIdNumBuyer1 = (EditText)findViewById(R.id.updateIdNumBuyer);

    Button updateInfo = (Button)findViewById(R.id.updatInfoButton);
    Button backButtonUpdate = (Button)findViewById(R.id.backButton);

   //update button write to file
       updateInfo.setOnClickListener(new OnClickListener(){
        public void onClick(View v){
       String sendIdBuyer = updateIdNumBuyer1.getText().toString();


      MainActivity sendData = new MainActivity();
      try {
        sendData.writeToFile(sendIdBuyer);
    } catch (FileNotFoundException e1) {
        e1.printStackTrace();
        Toast.makeText(UpdateScreen.this, 
                "bombs on updatescreen", Toast.LENGTH_LONG).show(); 
    }

    }
       });// End of update

        //Back to main screen button
        backButtonUpdate.setOnClickListener(new OnClickListener(){
            public void onClick(View v){
                startActivity(new Intent(UpdateScreen.this, MainActivity.class));
            }
        });// End back button
}
}

1 个答案:

答案 0 :(得分:0)

MainActivity sendData = new MainActivity(); 

无法使用new运算符创建Activity的实例。只能使用Intent创建活动。

public void writeToFile() 

将其更改为

public static void writeToFile() 

然后调用其他活动,如

MainActivity.writeToFile(....). 

可能你必须添加一个Context参数才能使它工作。