我正在构建一个基本应用程序,您可以从微调器中选择一种颜色并单击“保存”。如果您要查找的颜色不在微调框中,请按下按钮,然后将其输入到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