0

我是android开发的新手。我只是在做一个简单的项目来保存每次搜索栏移动时输入的文本的字体大小。

我没有错误地完成了项目,但是当我运行应用程序时,它在模拟器中显示错误消息说“不幸的是 SharedPreferences 已停止”。

我也在下面的链接中搜索了这个并应用了所有但在我的情况下没有这样的错误:

  1. 不幸的是,应用程序已在 Android 模拟器中停止
  2. 不幸的是 HelloListView 已停止
  3. 不幸的是,*应用名称*已停止
  4. 不幸的是,MyApp 已停止在模拟器中
  5. 不幸的是,应用程序已在模拟器中停止
  6. 不幸的是,应用程序已停止
  7. 不幸的是 Project_Name 已停止
  8. “不幸的是,应用名称已停止。” 安卓设备或模拟器

还有更多。

这是项目的 LAYOUT 文件(sharedprefer.xml):

<Seekbar android:id="@+id/sb"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content" />

<TextView
    android:id="@+id/tv"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/hello_world" />

<EditText android:id="@+id/et"
    android:hint="@string/txt"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content" />

<Button
    android:id="@+id/b"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="@string/s" />

.Java 文件是(Main.java):

导入android.app.Activity;导入 android.content.SharedPreferences;导入android.os.Bundle;导入 android.view.Menu;导入android.view.View;导入android.widget.Button;导入 android.widget.EditText;导入 android.widget.SeekBar;导入 android.widget.SeekBar.OnSeekBarChangeListener;导入 android.widget.Toast;

公共类主要扩展活动{

private SharedPreferences pref;
private String pName = "MyPref";
private EditText et;
private SeekBar sb;
private Button b;

private static final String FONT_SIZE_KEY = "fontsize";
private final static String TEXT_VALUE_KEY = "textvalue";


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

    et = (EditText) findViewById(R.id.et);
    sb = (SeekBar) findViewById(R.id.sb);
    b = (Button) findViewById(R.id.b);

    b.setOnClickListener(new View.OnClickListener(){
        public void onClick(View v){
            pref = getSharedPreferences(pName, MODE_PRIVATE);
            SharedPreferences.Editor editor = pref.edit();
            editor.putFloat(FONT_SIZE_KEY, et.getTextSize());
            editor.putString(TEXT_VALUE_KEY, et.getText().toString());

            editor.commit();

            Toast.makeText(getBaseContext(), "Saved" , Toast.LENGTH_LONG).show();
        }
    });

    pref = getSharedPreferences(pName, MODE_PRIVATE);
    float fontsize = pref.getFloat(FONT_SIZE_KEY, 14);

    sb.setProgress((int)fontsize);
    et.setText(pref.getString(TEXT_VALUE_KEY, ""));
    et.setTextSize(sb.getProgress());

    sb.setOnSeekBarChangeListener(new OnSeekBarChangeListener(){

        @Override
        public void onProgressChanged(SeekBar sbar, int progress, boolean fromUser) {
            // TODO Auto-generated method stub
            et.setTextSize(progress);
        }

        @Override
        public void onStartTrackingTouch(SeekBar seekBar) {
            // TODO Auto-generated method stub

        }

        @Override
        public void onStopTrackingTouch(SeekBar seekBar) {
            // TODO Auto-generated method stub

        }
    });
}

@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;
}

}

我发现 LOGCAT 中的错误是:

07-17 13:12:18.369: I/Process(576): Sending signal. PID: 576 SIG: 9
07-17 13:12:30.760: D/AndroidRuntime(622): Shutting down VM
07-17 13:12:30.760: W/dalvikvm(622): threadid=1: thread exiting with uncaught exception (group=0x409961f8)
07-17 13:12:30.810: E/AndroidRuntime(622): FATAL EXCEPTION: main
07-17 13:12:30.810: E/AndroidRuntime(622): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.nsa.sharedpreferences/com.nsa.sharedpreferences.Main}: android.view.InflateException: Binary XML file line #7: Error inflating class Seekbar
07-17 13:12:30.810: E/AndroidRuntime(622):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1955)
07-17 13:12:30.810: E/AndroidRuntime(622):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1980)
07-17 13:12:30.810: E/AndroidRuntime(622):  at android.app.ActivityThread.access$600(ActivityThread.java:122)
07-17 13:12:30.810: E/AndroidRuntime(622):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1146)
07-17 13:12:30.810: E/AndroidRuntime(622):  at android.os.Handler.dispatchMessage(Handler.java:99)
07-17 13:12:30.810: E/AndroidRuntime(622):  at android.os.Looper.loop(Looper.java:137)
07-17 13:12:30.810: E/AndroidRuntime(622):  at android.app.ActivityThread.main(ActivityThread.java:4340)
07-17 13:12:30.810: E/AndroidRuntime(622):  at java.lang.reflect.Method.invokeNative(Native Method)
07-17 13:12:30.810: E/AndroidRuntime(622):  at java.lang.reflect.Method.invoke(Method.java:511)
07-17 13:12:30.810: E/AndroidRuntime(622):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
07-17 13:12:30.810: E/AndroidRuntime(622):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
07-17 13:12:30.810: E/AndroidRuntime(622):  at dalvik.system.NativeStart.main(Native Method)
07-17 13:12:30.810: E/AndroidRuntime(622): Caused by: android.view.InflateException: Binary XML file line #7: Error inflating class Seekbar
07-17 13:12:30.810: E/AndroidRuntime(622):  at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:691)
07-17 13:12:30.810: E/AndroidRuntime(622):  at android.view.LayoutInflater.rInflate(LayoutInflater.java:739)
07-17 13:12:30.810: E/AndroidRuntime(622):  at android.view.LayoutInflater.inflate(LayoutInflater.java:489)
07-17 13:12:30.810: E/AndroidRuntime(622):  at android.view.LayoutInflater.inflate(LayoutInflater.java:396)
07-17 13:12:30.810: E/AndroidRuntime(622):  at android.view.LayoutInflater.inflate(LayoutInflater.java:352)
07-17 13:12:30.810: E/AndroidRuntime(622):  at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:251)
07-17 13:12:30.810: E/AndroidRuntime(622):  at android.app.Activity.setContentView(Activity.java:1835)
07-17 13:12:30.810: E/AndroidRuntime(622):  at com.nsa.sharedpreferences.Main.onCreate(Main.java:29)
07-17 13:12:30.810: E/AndroidRuntime(622):  at android.app.Activity.performCreate(Activity.java:4465)
07-17 13:12:30.810: E/AndroidRuntime(622):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
07-17 13:12:30.810: E/AndroidRuntime(622):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1919)
07-17 13:12:30.810: E/AndroidRuntime(622):  ... 11 more
07-17 13:12:30.810: E/AndroidRuntime(622): Caused by: java.lang.ClassNotFoundException: android.view.Seekbar
07-17 13:12:30.810: E/AndroidRuntime(622):  at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:61)
07-17 13:12:30.810: E/AndroidRuntime(622):  at java.lang.ClassLoader.loadClass(ClassLoader.java:501)
07-17 13:12:30.810: E/AndroidRuntime(622):  at java.lang.ClassLoader.loadClass(ClassLoader.java:461)
07-17 13:12:30.810: E/AndroidRuntime(622):  at android.view.LayoutInflater.createView(LayoutInflater.java:552)
07-17 13:12:30.810: E/AndroidRuntime(622):  at android.view.LayoutInflater.onCreateView(LayoutInflater.java:636)
07-17 13:12:30.810: E/AndroidRuntime(622):  at com.android.internal.policy.impl.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:66)
07-17 13:12:30.810: E/AndroidRuntime(622):  at android.view.LayoutInflater.onCreateView(LayoutInflater.java:653)
07-17 13:12:30.810: E/AndroidRuntime(622):  at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:678)
07-17 13:12:30.810: E/AndroidRuntime(622):  ... 21 more

请告诉我它的解决方案是什么。

4

1 回答 1

4

改变

<Seekbar android:id="@+id/sb"

<SeekBar android:id="@+id/sb"

必须b是资本B

于 2013-07-17T07:50:05.947 回答