我正在構建一個基本應用程序,您可以從微調器中選擇一種顏色並單擊「保存」。如果您要查找的顏色不在微調框中,請按下按鈕,然後將其輸入到EditText中。 我試圖讓它可以寫入所選的微調器/編輯文本顏色和「colors.txt」文件中的日期。目前,沒有任何錯誤,但是當我在Android上啓動應用程序時,它立即崩潰。將多個字符串寫入文件並添加新行
Colorchooser.java
package com.example.scalendar;
import java.io.IOException;
import java.io.OutputStreamWriter;
import android.os.Bundle;
import android.app.Activity;
import android.content.Context;
import android.util.Log;
import android.view.Menu;
public class Colorchooser extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_colorchooser);
}
public void writetoFile(String str) {
try {
OutputStreamWriter outputStreamWriter = new OutputStreamWriter(openFileOutput("colors.txt", Context.MODE_PRIVATE));
outputStreamWriter.write("colorstring , datestring");
outputStreamWriter.close();
}
catch (IOException e) {
Log.e("Exception", "File write failed: " + e.toString());
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.colorchooser, menu);
return true;
}
}`
這裏是我的strings.xml文件
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="app_name">SCalendar</string>
<string name="action_settings">Settings</string>
<string name="color_prompt">What was todays color?</string>
<string name="SaveButtonText">Save To Calendar</string>
<string name="textfieldhint">Enter color</string>
<string name="otherbuttontext">Color not listed</string>
<string name="othercolorexplanationtext">Dont see the color here? Click the button below and type it yourself.</string>
<string-array name="color_arrays">
<item>Select a color:</item>
<item>Purple</item>
<item>Orange</item>
<item>Green</item>
<item>Red</item>
<item>Blue</item>
<item>Pink</item>
<item>Brown</item>
<item>Yellow</item>
<item>Black</item>
<item>White</item>
</string-array>
<string name="colorstring">
colorSpinner = findViewById(R.id.colorSpinner)
colorSpinner.getSelectedItem().toString()
</string>
<string name="datestring">
SimpleDateFormat formatter = new SimpleDateFormat("dd_MM_yyyy");
Date now = new Date();
String fileName = formatter.format(now).toString();
</string>
</resources>`
這是我的Mainactivity.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="Colorchooser" >
<Spinner
android:id="@+id/colorSpinner"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:entries="@array/color_arrays"
android:prompt="@string/color_prompt" />
<TextView
android:id="@+id/othercolorexplaination"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/colorSpinner"
android:layout_below="@+id/colorSpinner"
android:layout_marginTop="15dp"
android:text="@string/othercolorexplanationtext"
android:textAppearance="?android:attr/textAppearanceMedium" />
<Button
android:id="@+id/ButtonSave"
style="@style/AppTheme"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/othercolorexplaination"
android:layout_alignParentBottom="true"
android:layout_marginBottom="62dp"
android:onClick="writetoFile(String getcolor)"
android:text="@string/SaveButtonText" />
<EditText
android:id="@+id/othercolor"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/ButtonSave"
android:layout_below="@+id/otherbutton"
android:layout_marginTop="42dp"
android:ems="10"
android:focusable="false"
android:hint="@string/textfieldhint"
android:inputType="textEmailAddress" >
</EditText>
<Button
android:id="@+id/otherbutton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/othercolorexplaination"
android:layout_centerHorizontal="true"
android:layout_marginTop="24dp"
android:onClick="othercolor.focusableInTouchMode(true)"
android:text="@string/otherbuttontext" />
</RelativeLayout>`
如果應用程序崩潰,請發佈您的Logcat。 – Prmths
請在發佈之前閱讀logcat。如果你不理解它,搜索類似的問題。如果這沒有幫助,請回到這裏。 – keyser
老實說,上帝不知道如何使用登錄貓。我在我的銀河S3上下載了alogcat,即使啓動它,它也沒有向我顯示與應用有關的任何內容。 – user3190265