简体   繁体   中英

I am unable to start a new activity through Intent in Android

Unable to start new activity(ie OpenItDude.class).

My app is about saving/writing a .txt file in external storage and then reading it .

All works fine but when I press open button the app crashes. Save button works absolutely fine. And when I press open button after pressing save button app doesn't crashes but nothing happens... Unable to start new activity - OpenItDude.class

MainActivity.class

public class MainActivity extends Activity {

EditText message,fileName;
Button save,open;
String msg,file;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setIt("","");
    final Intent ourIntent = new Intent(MainActivity.this,OpenItDude.class);
    save.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            msg = message.getText().toString();
            file = fileName.getText().toString();
            save(msg,file);

        }
    });

    open.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            startActivity(ourIntent);

        }
    });



}

void save(String msg1,String file1)
{

    try
    {
        File notes = new File(Environment.getExternalStorageDirectory(), "Notes");
        if (!notes.exists()) {
            notes.mkdirs();
        }
        File gpxfile = new File(notes, file1+".txt");
        FileWriter writer = new FileWriter(gpxfile);
        writer.append(msg1);
        writer.flush();
        writer.close();
        Toast.makeText(getApplicationContext(), "Saved !",
                Toast.LENGTH_SHORT).show();
    }
    catch(IOException e)
    {
        e.printStackTrace();
        Toast.makeText(getApplicationContext(), "File Not Saved !",
                Toast.LENGTH_SHORT).show();
    }
    setIt("", "");
}

void setIt(String name,String message){
    setContentView(R.layout.activity_main);

    fileName = (EditText)findViewById(R.id.filename);
    this.message=(EditText)findViewById(R.id.editText);
    save= (Button) findViewById(R.id.button);
    open = (Button) findViewById(R.id.openbtn);

    fileName.setText(name);
    this.message.setText(message);

}

@Override
protected void onPause() {
    super.onPause();
    finish();
}
}

OpenItDude.class

public class OpenItDude extends Activity {

Button opens, save;
AutoCompleteTextView actv;
String fileToOpen,MSG,FILE;
EditText FN, ET;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.openfilelayout);

    opens = (Button) findViewById(R.id.button2open);
    actv = (AutoCompleteTextView) findViewById(R.id.autoCompleteTextView);
    FN = (EditText) findViewById(R.id.FN);
    ET = (EditText) findViewById(R.id.ET);
    save = (Button) findViewById(R.id.save);

    opens.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            //make open button logic
            //Work is pending...
            fileToOpen = actv.getText().toString();
            try {
                FileInputStream fin = openFileInput(fileToOpen);
                int c;
                String temp = "";

                while ((c = fin.read()) != -1) {
                    temp = temp + Character.toString((char) c);
                }
                setContentView(R.layout.open_ultimate);
                FN.setText(fileToOpen);
                ET.setText(temp);
                Toast.makeText(getBaseContext(), "File read !", Toast.LENGTH_SHORT).show();



            } catch (Exception e) {
                e.printStackTrace();
            }

        }
    });

    save.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            MSG = ET.getText().toString();
            FILE = FN.getText().toString();
            save(MSG,FILE);

        }
    });

}

void save(String msg1,String file1)
{

    try
    {
        File notes = new File(Environment.getExternalStorageDirectory(), "Notes");
        if (!notes.exists()) {
            notes.mkdirs();
        }
        File gpxfile = new File(notes, file1+".txt");
        FileWriter writer = new FileWriter(gpxfile);
        writer.append(msg1);
        writer.flush();
        writer.close();
        Toast.makeText(getApplicationContext(), "Saved !",
                Toast.LENGTH_SHORT).show();
    }
    catch(IOException e)
    {
        e.printStackTrace();
        Toast.makeText(getApplicationContext(), "File Not Saved !",
                Toast.LENGTH_SHORT).show();
    }
    Intent myIntent = new Intent(OpenItDude.this,MainActivity.class);
    startActivity(myIntent);
}

@Override
protected void onPause() {
    super.onPause();
    finish();
}
}`

Manifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.corei5.noteitdown" >

<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:supportsRtl="true"
    android:theme="@style/AppTheme" >
    <activity android:name=".MainActivity" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <activity android:name=".OpenItDude">

        </activity>
</application>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />

</manifest>

Your attempting to start the activity from the context of the click listener. Try v.getContext().startActivity(ourIntent); instead.

When you click save button, setIt() is called, which re-initialize the content view of the activity. But click listeners for the views are not set again. That's why nothing happens when clicking open after clicking save.

And you can try this to start OpenItDude activity:

open.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        startActivity(new Intent(MainActivity.this, OpenItDude.class));
    }
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM