简体   繁体   English

不幸的是,应用程序已停止,android

[英]unfortunately app has stopped , android

i have given the code here .我在这里给出了代码。 and when i run it in genymotion it just stops working.当我在 genymotion 中运行它时,它就停止工作了。 please help .请帮忙 。 i dont see any display in logcat also .我也没有在 logcat 中看到任何显示。 it remains blank and the emulator only shows an error that is "unfortulately, SharedprefrencEexample has stoped working. and the app dont even starts. so i dont get any clue of what might be the error in this code . here i have used the reference creation at the start and i think it might be the error. so please do see the code and help me. thanks.它保持空白,模拟器只显示一个错误,“不幸的是,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活动_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 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>
submit.setText("@string/button_name_activity_one");
// (...)
sharedPreferences=getSharedPreferences("@string/database_name", MODE_PRIVATE);

That syntax is only valid in XML.该语法仅在 XML 中有效。 In Java code, to use a string reference you should use在 Java 代码中,要使用字符串引用,您应该使用

submit.setText(R.string.button_name_activity_one);

So fix those string references and it should work.所以修复这些字符串引用,它应该可以工作。

More info: String resources @ Android Developers更多信息: 字符串资源@Android 开发者

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM