单击“写入”按钮时Android应用程序强制关闭

时间:2013-12-10 09:48:18

标签: android

谢谢大家,我希望我能为我的错误找到解决方案。

public class MainActivity extends Activity {

 public void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);


       Button mute=(Button)findViewById(R.id.button1);
       mute.setOnClickListener(new View.OnClickListener() 
       {

        @Override
        public void onClick(View v) 
        {
            Intent write=new Intent("com.senior.senior.WRITE");
            startActivity(write);

        }
    });
    }
}

XML主要活动

<LinearLayout 

    xmlns:android="http://schemas.android.com/apk/res/android"
      android:layout_width="fill_parent"
      android:layout_height="fill_parent"
      android:layout_above="@+id/textView1"
      android:layout_toLeftOf="@+id/textView1"
      android:gravity="center"
      android:layout_gravity="center"
      android:orientation="vertical"
      android:background="#000000" >

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="55dp"
        android:text="Write" />

      <Button
          android:id="@+id/button2"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:layout_alignRight="@+id/button1"
          android:layout_below="@+id/button1"
          android:layout_marginTop="66dp"
          android:text="Read" />

</LinearLayout>

    public class Write extends Activity implements  OnClickListener{

EditText text;
TextView retrive1;
public static String filename="Mysharedstring" ;
SharedPreferences someData;

@Override
protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    setContentView(R.layout.write);
    setupVariables();
    someData = getSharedPreferences(filename, 0);

    }

private void setupVariables()
{
    Button sav= (Button) findViewById(R.id.save);
    Button ret= (Button) findViewById(R.id.retrive);
     text= (EditText) findViewById(R.id.txtText);
    retrive1= (TextView) findViewById(R.id.textview);
    ret.setOnClickListener(this);
    sav.setOnClickListener(this);

}

public void onClick(View v)
{
    switch(v.getId()){
    case  R.id.save:
    String stringdata= text.getText().toString();
    SharedPreferences.Editor editor = someData.edit();
    editor.putString("sharedString", stringdata);
    editor.commit();
    break;

    case R.id.retrive:
        someData = getSharedPreferences(filename, 0);
  String datareturned = someData.getString("sharedString", "couldn't load");
  retrive1.setText(datareturned);
        break;
    }
}   
}

写XML

<?xml version="1.0" encoding="utf-8"?>
 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_above="@+id/textView1"
android:layout_gravity="center"
android:layout_toLeftOf="@+id/textView1"
android:background="#000000"
android:gravity="center"
android:orientation="vertical" >

<EditText
    android:id="@+id/txtText"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginLeft="10dp"
    android:layout_marginRight="10dp"
    android:layout_marginTop="10dp"
    android:hint="Enter Your Text Here"
    android:textColor="#000000" />

<Button
    android:id="@+id/save"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="save"
    android:textColor="#FFFFFF" />

<Button
    android:id="@+id/retrive"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="retrive"
    android:textColor="#FFFFFF" />

<TextView
    android:id="@+id/textview"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginLeft="10dp"
    android:layout_marginRight="10dp"
    android:layout_marginTop="10dp"
    android:text="Text To Speech"
    android:textAppearance="?android:attr/textAppearanceLarge"
    android:textColor="#FFFFFF" />

</LinearLayout>

2 个答案:

答案 0 :(得分:1)

试试这个:

Intent i= new Intent(this, WRITE.class);//assuming WRITE is the name of the activity to launch, don't forget to import if it's in another package
startActivity(i);

添加活动以调用清单(MainActivity也应该在那里)

<application

        <activity
            android:name="com.senior.senior.WRITE"
         </activity>

答案 1 :(得分:0)

您需要在创建Intent对象时添加上下文。

试试这一行:

Intent write = new Intent(this, "com.senior.senior.WRITE");