這是一所學校的項目,我按下保存,它被保存,當我按下Load按鈕應該在TextView的負荷(標籤)與生成APK問題不斷崩潰
的問題是,當我產生該apk安裝到我的手機上,只要我點擊任何按鈕,程序崩潰。
如果我在文本字段中輸入任何內容,則無關緊要。
我不知道有什麼問題,請幫忙!
我必須在我的表單上運行apk,所以在虛擬機上運行它首先進行測試是不可能的。
我測試了它在兩個設備兩者產生相同的結果
我的代碼:
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import java.io.FileInputStream;
import java.io.FileOutputStream;
public class MainActivity extends AppCompatActivity {
private String file = "mydatabase";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void Save(View view) {
EditText save = (EditText) findViewById(R.id.txt_save);
String data = save.getText().toString();
try {
FileOutputStream fOut = openFileOutput(file, MODE_PRIVATE);
fOut.write(data.getBytes());
fOut.close();
Toast.makeText(getBaseContext(), "File saved sucessfully", Toast.LENGTH_SHORT).show();
} catch (Exception e) {
e.printStackTrace();
}
}
public void load (View view) {
TextView load = (TextView) findViewById(R.id.lbl_Display);
try {
FileInputStream fin = openFileInput(file);
int c;
String temp = "";
while ((c = fin.read()) != -1) {
temp = temp + Character.toString((char) c);
}
load.setText(temp);
Toast.makeText(getBaseContext(), "File Read Sucessfully", Toast.LENGTH_SHORT).show();
} catch (Exception e) {
}
}}
這裏是我使用的UI,其中txt_save是文本字段和lbl_Display是TextView的
錯誤:
W/dalvikvm: threadid=1: thread exiting with uncaught exception (group=0xb17c34f0)
E/AndroidRuntime: FATAL EXCEPTION: main
java.lang.IllegalStateException: Could not find method Save (MainActivity)(View) in a parent or ancestor Context for android:onClick attribute defined on view class android.support.v7.widget.AppCompatButton with id 'button'
at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.resolveMethod(AppCompatViewInflater.java:327)
at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:284)
at android.view.View.performClick(View.java:2485)
at android.view.View$PerformClick.run(View.java:9080)
at android.os.Handler.handleCallback(Handler.java:587)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:130)
at android.app.ActivityThread.main(ActivityThread.java:3683)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:507)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
at dalvik.system.NativeStart.main(Native Method)
Application terminated.
我使用薑餅(API等級10)
我的清單
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.user.mydatabase">
<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>
</application>
</manifest>
份額粗魯的報告日誌。 –
請將權限作爲 添加到您的清單文件中,然後嘗試運行。 –
Priyavrat
激動地在清單文件的哪裏寫下它?在哪個標題下 – chloe