我无法通过Android中的Intent启动新活动

时间:2015-12-21 13:58:08

标签: android android-intent android-activity

无法启动新活动(即OpenItDude.class)。

我的应用程序是关于在外部存储中保存/写入.txt文件然后阅读它。

一切正常,但当我按下打开按钮时,应用程序崩溃了。保存按钮工作正常。当按下保存按钮后我按下打开按钮应用程序没有崩溃但没有任何反应...无法启动新活动 - 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>

2 个答案:

答案 0 :(得分:0)

您尝试从点击侦听器的上下文启动活动。请尝试使用v.getContext().startActivity(ourIntent);

答案 1 :(得分:0)

单击“保存”按钮时,将调用setIt(),重新初始化活动的内容视图。但是没有再次设置视图的单击侦听器。这就是单击保存后单击打开时没有任何反应的原因。

你可以试试这个来开始OpenItDude活动:

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