SaveButton被破坏了

时间:2013-06-07 15:10:33

标签: android eclipse button crash

我的代码如下。我的问题是为什么我的“SaveButton”被破坏,意味着当我点击它时,我的应用程序崩溃了。如果我按下“ViewButton”,我会添加另一个按钮,这不是应该实际添加按钮的正确按钮。     很抱歉长代码,但它真的很重要:)。还是android的新手。     非常感谢您的回答!

 package com.arise.filewritereader;


import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import android.os.Bundle;
import android.app.Activity;
import android.graphics.Typeface;
import android.view.Menu;
import android.view.View.OnClickListener;
import android.view.View;
import android.widget.AbsoluteLayout;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Button button = (Button) findViewById(R.id.button3);
        button.setOnClickListener(new OnClickListener() {
            public void onClick(View v) {
                createNewButton();
            }
        });
        //Button button1= (Button)findViewById(R.id.button1);
        //Typeface typeFace=Typeface.createFromAsset(getAssets(), "AnjaliOldLipi.ttf");
        //button1.setTypeface(typeFace);
         //button1.setText("à´…");
    }

     public void SaveText(View view){    

         try {
              // open myfilename.txt for writing
              OutputStreamWriter out = new OutputStreamWriter(openFileOutput("myfilename.txt",MODE_APPEND));
              // write the contents on mySettings to the file
              EditText ET = (EditText)findViewById(R.id.editText1);
              String text = ET.getText().toString();
               out.write(text);
               out.write('\n');            
              // close the file
              out.close();
              Toast.makeText(this,"Text Saved !",Toast.LENGTH_LONG).show();
            } catch (java.io.IOException e) {
              //do something if an IOException occurs.
                Toast.makeText(this,"Sorry Text could't be added",Toast.LENGTH_LONG).show();
            }

     }

     public void ViewText (View view){  


     StringBuilder text = new StringBuilder();


     try {
            // open the file for reading we have to surround it with a try

            InputStream instream = openFileInput("myfilename.txt");//open the text file for reading

            // if file the available for reading
            if (instream != null) {             

              // prepare the file for reading
              InputStreamReader inputreader = new InputStreamReader(instream);
              BufferedReader buffreader = new BufferedReader(inputreader);

              String line=null;
              //We initialize a string "line" 

            while (( line = buffreader.readLine()) != null) {
                //buffered reader reads only one line at a time, hence we give a while loop to read all till the text is null

                            text.append(line);  
                            text.append('\n');  //to display the text in text line      


              }}}    

            //now we have to surround it with a catch statement for exceptions
            catch (IOException e) {
                e.printStackTrace();
            }

            //now we assign the text readed to the textview
            TextView tv = (TextView)findViewById(R.id.textView1);                       
            tv.setText(text);       

        }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.activity_main, menu);
        return true;
    }


    public void createNewButton()
    {
        RelativeLayout mainLayout = (RelativeLayout)findViewById(R.id.activity_main);
        Button addButton =new Button(this);
        addButton.setText("add");
        mainLayout.addView(addButton);
    }
    }


    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"

    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity" >

    <RelativeLayout

        android:id="@+id/linearLayout1"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_x="28dp"
        android:layout_y="60dp"
        android:gravity="center" >
    </RelativeLayout>

    <Button
        android:id="@+id/button3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:text="neuerKnopf" />

    <EditText
        android:id="@+id/editText1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_above="@+id/button3"
        android:layout_marginBottom="26dp"
        android:layout_toRightOf="@+id/button1"
        android:ems="10"
        android:inputType="textPostalAddress" >

        <requestFocus />
    </EditText>

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_above="@+id/editText1"
        android:layout_marginBottom="34dp"
        android:layout_toLeftOf="@+id/button3"
        android:onClick="SaveText"
        android:text="Save" />

    <Button
        android:id="@+id/button2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBottom="@+id/button1"
        android:layout_alignRight="@+id/editText1"
        android:layout_marginRight="56dp"
        android:onClick="ViewText"
        android:text="View" />

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_alignRight="@+id/button3"
        android:layout_marginBottom="75dp"
        android:text="TextView" />

</RelativeLayout>

0 个答案:

没有答案