不幸的是app已经停止了,android

时间:2015-09-06 23:09:15

标签: java android error-handling app-store

我在这里给出了代码。当我在genymotion中运行它时它就会停止工作。请帮忙 。 我也没有在logcat中看到任何显示。它仍然是空白的,模拟器只显示一个错误,“不幸的是,SharedprefrencEexample已停止工作。应用程序甚至没有启动。所以我不知道这个代码中可能出现的错误。 这里我在开始时使用了参考创建,我认为这可能是错误。所以请看看代码并帮助我。 感谢。

Main_Activity

package com.example.sharedprefrenceexample;

import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;

public class MainActivity extends Activity {

EditText name,phone,email;
Button submit;
Intent intent;
SharedPreferences sharedPreferences;
SharedPreferences.Editor editor;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    name=(EditText) findViewById(R.id.editText1);
    phone=(EditText) findViewById(R.id.editText2);
    email=(EditText) findViewById(R.id.editText3);
    submit=(Button) findViewById(R.id.button1);

    submit.setText("@string/button_name_activity_one");
    name.setHint("name");
    phone.setHint("phone");
    email.setHint("email id");

    sharedPreferences=getSharedPreferences("@string/database_name", MODE_PRIVATE);
    editor=sharedPreferences.edit();

    submit.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View arg0) {
            // TODO Auto-generated method stub
            editor.putString("name", name.getText().toString());
            editor.putString("phone", phone.getText().toString());

            editor.commit();

            intent=new Intent(MainActivity.this,Secondactivity.class);
            intent.putExtra("email", email.getText().toString());

            startActivity(intent);
        }
    });
}

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

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();
    if (id == R.id.action_settings) {
        return true;
    }
    return super.onOptionsItemSelected(item);
}
}

activity_main.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="com.example.sharedprefrenceexample.MainActivity" >

<EditText
    android:id="@+id/editText1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_alignParentTop="true"
    android:ems="10" >

    <requestFocus />
</EditText>

<EditText
    android:id="@+id/editText2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_below="@+id/editText1"
    android:ems="10"
    android:inputType="numberPassword" />

<EditText
    android:id="@+id/editText3"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/editText2"
    android:layout_below="@+id/editText2"
    android:ems="10"
    android:inputType="textEmailAddress" />

<Button
    android:id="@+id/button1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:layout_alignParentRight="true"
    android:text="@string/button_name_activity_one" />

</RelativeLayout>

secondactivity

package com.example.sharedprefrenceexample;

import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.TextView;

public class Secondactivity extends Activity {

TextView result;
SharedPreferences sharedPreferences;
Intent intent;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_secondactivity);

    result=(TextView) findViewById(R.id.textView1);

    sharedPreferences=getSharedPreferences("database", MODE_PRIVATE);
    String res = null;
    res=res+sharedPreferences.getString("name", "no name");
    res=res+sharedPreferences.getString("phone", "no phone");

    intent=getIntent();

    res=res+intent.getStringExtra("email");

    result.setText(res);
}

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

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();
    if (id == R.id.action_settings) {
        return true;
    }
    return super.onOptionsItemSelected(item);
}
}

activity_secondactivity.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="com.example.sharedprefrenceexample.Secondactivity" >

<TextView
    android:id="@+id/textView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_alignParentTop="true"
    android:text="Large Text"
    android:textAppearance="?android:attr/textAppearanceLarge" />

</RelativeLayout>

1 个答案:

答案 0 :(得分:0)

submit.setText("@string/button_name_activity_one");
// (...)
sharedPreferences=getSharedPreferences("@string/database_name", MODE_PRIVATE);

该语法仅在XML中有效。在Java代码中,要使用字符串引用,您应该使用

submit.setText(R.string.button_name_activity_one);

所以修复那些字符串引用,它应该可以工作。

更多信息:String resources @ Android Developers

相关问题