2014-01-13 2 views
0

나는 스피너에서 선택한 기본 앱을 만들고 색상을 선택하고 저장을 클릭합니다. 찾고있는 색상이 회 전자에 없으면 단추를 누르고 EditText에 입력 할 수 있습니다. "colors.txt"파일 안에 선택한 스피너/편집 문구 색과 날짜를 모두 쓸 수 있도록 만들기 위해 노력 중입니다. 현재로서는 오류는 없지만 내 안드로이드에서 앱을 실행하면 순간적으로 충돌이 발생합니다. 여기 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>` 
+1

응용 프로그램이 충돌하는 경우 Logcat을 게시하십시오. – Prmths

+0

게시하기 전에 logcat을 읽으십시오. 그리고 만약 당신이 그것을 이해할 수 없다면 비슷한 문제를 찾아야합니다. 도움이 안되면 여기로 돌아와. – keyser

+0

나는 신에게 정직하게 로그 고양이를 사용하는 방법을 모른다. 내 galaxy s3에 alogcat을 다운로드했고, 앱을 시작한 후에도 앱과 관련된 모든 것을 보여주지 않습니다. 알 겠어. – user3190265

답변

1

입니다 자바 문자열 대 문자열 var 사이의 관계에 대한 오해 문자열 리소스 대 iables 및 문자열 변수 :

  1. 이 :

    outputStreamWriter.write("colorstring , datestring"); 
    

    는 항상 파일에 "colorstring, 날짜 열"을 쓸 것이다, 그래서 파일은 다음과 같이 표시됩니다

    colorstring, datestring 
    colorstring, datestring 
    colorstring, datestring 
    
  2. 문자열 리소스의 Java 코드가 실행될 것으로 예상 할 수 없습니다.

    <string name="colorstring"> 
         colorSpinner = findViewById(R.id.colorSpinner) 
         colorSpinner.getSelectedItem().toString() 
    </string> 
    

    리소스의 문자열은 Constants입니다. 런타임에 변경할 방법이 없습니다.

+0

. colors.txt 파일에 날짜 및 회 전자/EditText 필드 데이터를 추가하는 방법은 무엇입니까? – user3190265