Android 操作共享首选项时出错?

Android 操作共享首选项时出错?,android,sharedpreferences,Android,Sharedpreferences,我希望免责声明在安装我的应用程序后第一次出现! 在我的主要活动中,我有两个按钮:第一个“同意”按钮将在共享首选项中存储布尔值“真”值,并将其重定向到Welcome.class中的u!第二个按钮将终止应用程序! 此情况应在安装应用程序后第一次出现! 我找不到我的错误 MainActivity.java代码: public class MainActivity extends Activity implements OnClickListener {public static

我希望免责声明在安装我的应用程序后第一次出现! 在我的主要活动中,我有两个按钮:第一个“同意”按钮将在共享首选项中存储布尔值“真”值,并将其重定向到Welcome.class中的u!第二个按钮将终止应用程序! 此情况应在安装应用程序后第一次出现! 我找不到我的错误

MainActivity.java代码:

    public class MainActivity extends Activity implements OnClickListener    
    {public static String filename = "MySharedString";
SharedPreferences settings;
Button agree,disagree;
public static final String PREFS_NAME = "MyPrefsFile";

@Override
protected void onCreate(Bundle state)
{
    super.onCreate(state);  
    setContentView(R.layout.activity_main);
    TextView text1=(TextView)findViewById(R.id.txt1);
    TextView text2 = (TextView)findViewById(R.id.txt2);
    text2.setText("Android custom dialog example!");
    ImageView image = (ImageView)findViewById(R.id.imageView1);
    image.setImageResource(R.drawable.ic_launcher);
    Button agreeButton = (Button)findViewById(R.id.button1);
    Button disagreeButton = (Button)findViewById(R.id.button2);
    agreeButton.setOnClickListener(this);
    disagreeButton.setOnClickListener(this);
    settings = getSharedPreferences(filename, 0);
}

@Override
public void onClick(View v)
{
    switch (v.getId())
    {
    case R.id.button1:
        SharedPreferences settings = getSharedPreferences(filename, 0);
        boolean agreement = settings.getBoolean("agreement",false);

        SharedPreferences.Editor editor = settings.edit();
        editor.putBoolean("agreement", true);
        editor.commit();

        Intent i=new Intent(getApplicationContext(),Welcome.class);
                    startActivity(i);
        break;

         case R.id.button2:
             finish();
             System.exit(0);
         }
     }
}
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:background="#d3d3d3"
tools:context=".MainActivity" >

<TextView
    android:id="@+id/txt1"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="Disclaimer" 
    android:gravity="center"
    android:textSize="20dp"
    android:layout_marginTop="5dp"/>

 <TextView
    android:id="@+id/txt2"
    android:layout_width="fill_parent"
    android:layout_height="50dp"
    android:text="This is a Disclaimer."
    android:textSize="10dp"
    android:layout_marginTop="10dp"
    android:layout_below="@+id/txt1"/>

 <Button
    android:id="@+id/button2"
    android:layout_width="100dp"
    android:layout_height="wrap_content"
    android:layout_alignBaseline="@+id/button1"
    android:layout_alignBottom="@+id/button1"
    android:layout_marginLeft="37dp"
    android:layout_toRightOf="@+id/button1"
    android:text="DisAgree" />

 <Button
    android:id="@+id/button1"
    android:layout_width="100dp"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_below="@+id/txt2"
    android:layout_marginLeft="46dp"
    android:text="Agree" />

 <ImageView
    android:id="@+id/imageView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_alignTop="@+id/txt1"
    android:layout_marginLeft="28dp"
    android:src="@drawable/ic_launcher" />

</RelativeLayout>
welcome.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >

<TextView
    android:id="@+id/textView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="WELCOME !"
    android:textAppearance="?android:attr/textAppearanceLarge" />

以下是AndroidManifest.xml代码:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.disclaimer1"
android:versionCode="1"
android:versionName="1.0" >

<uses-sdk
    android:minSdkVersion="8"
    android:targetSdkVersion="17" />

<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <activity
        android:name="com.example.disclaimer1.MainActivity"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
     <activity android:name="com.example.disclaimer1.Welcome"></activity>
</application>

</manifest>

日志:

10-13 07:23:30.069:E/AndroidRuntime(3356):致命异常:主 10-13 07:23:30.069:E/AndroidRuntime(3356):android.content.ActivityNotFoundException:找不到显式活动类{com.example.disclaimer1/com.example.disclaimer1.Welcome};您是否在AndroidManifest.xml中声明了此活动? 10-13 07:23:30.069:E/AndroidRuntime(3356):在android.app.Instrumentation.checkStartActivityResult(Instrumentation.java:1628) 10-13 07:23:30.069:E/AndroidRuntime(3356):在android.app.Instrumentation.execStartActivity(Instrumentation.java:1424) 10-13 07:23:30.069:E/AndroidRuntime(3356):在android.app.Activity.startActivityForResult(Activity.java:3390) 10-13 07:23:30.069:E/AndroidRuntime(3356):在android.app.Activity.startActivityForResult(Activity.java:3351) 10-13 07:23:30.069:E/AndroidRuntime(3356):在android.app.Activity.startActivity(Activity.java:3587) 10-13 07:23:30.069:E/AndroidRuntime(3356):在android.app.Activity.startActivity(Activity.java:3555) 10-13 07:23:30.069:E/AndroidRuntime(3356):在com.example.disclaimer1.MainActivity.onClick(MainActivity.java:54) 10-13 07:23:30.069:E/AndroidRuntime(3356):在android.view.view.performClick(view.java:4240)上 10-13 07:23:30.069:E/AndroidRuntime(3356):在android.view.view$PerformClick.run(view.java:17721) 10-13 07:23:30.069:E/AndroidRuntime(3356):在android.os.Handler.handleCallback(Handler.java:730)上 10-13 07:23:30.069:E/AndroidRuntime(3356):在android.os.Handler.dispatchMessage(Handler.java:92)上 10-13 07:23:30.069:E/AndroidRuntime(3356):在android.os.Looper.loop(Looper.java:137) 10-13 07:23:30.069:E/AndroidRuntime(3356):在android.app.ActivityThread.main(ActivityThread.java:5103)上 10-13 07:23:30.069:E/AndroidRuntime(3356):位于java.lang.reflect.Method.Invokenactive(本机方法) 10-13 07:23:30.069:E/AndroidRuntime(3356):位于java.lang.reflect.Method.invoke(Method.java:525) 10-13 07:23:30.069:E/AndroidRuntime(3356):在com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737) 10-13 07:23:30.069:E/AndroidRuntime(3356):在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
10-13 07:23:30.069:E/AndroidRuntime(3356):在dalvik.system.NativeStart.main(本机方法)

确保在AndroidManifest.xml文件中声明您的欢迎活动

最终答案

好吧,这不是最优雅的方式,但它会让你开始。之后,我建议您研究带有长文本的Ok/Cancel对话框,并使用其中一个而不是两个活动

main活动

public class MainActivity extends Activity implements OnClickListener 
{ 
    public static String filename = "MySharedString"; 
    public static final String PREFS_NAME = "MyPrefsFile"; 

@Override 
protected void onCreate(Bundle state) 
{ 
    super.onCreate(state); 
    setContentView(R.layout.main); 

    TextView text1=(TextView)findViewById(R.id.txt1); 
    TextView text2 = (TextView)findViewById(R.id.txt2);
    text2.setText("Android custom dialog example!"); 

    ImageView image = (ImageView)findViewById(R.id.imageView1);
    image.setImageResource(R.drawable.ic_launcher); 

    Button agreeButton = (Button)findViewById(R.id.button1);
    Button disagreeButton = (Button)findViewById(R.id.button2);

    agreeButton.setOnClickListener(this);
    disagreeButton.setOnClickListener(this); 

    boolean agree = getSharedPreferences(filename, 0).getBoolean("agreement", false);
    if(agree)
    {
        openWelcome();
    }           
}

@Override 
public void onClick(View v) 
{ 
    switch (v.getId()) 
    { 
        case R.id.button1: 
            SharedPreferences settings = getSharedPreferences(filename, 0);             
            SharedPreferences.Editor editor = settings.edit(); 

            editor.putBoolean("agreement", true); 
            editor.commit();        

            openWelcome();

            break; 

        case R.id.button2: 
            finish(); 
    }
}

private void openWelcome()
{
    Intent i=new Intent(this, Welcome.class);
    startActivity(i);

    finish();
    }
}

只是您首先必须在`SharedReference'中设置值,然后才可以访问它

请尝试以下操作:

 switch (v.getId())
    { 
    case R.id.button1:
        //Add the value in Sharepreference
        SharedPreferences.Editor editor = settings.edit();
        editor.putBoolean("agreement", true);
        editor.commit();

          //Get the value from it. 
          SharedPreferences settings = getSharedPreferences(filename, 0);
        boolean agreement = settings.getBoolean("agreement",false);

        Intent i=new Intent(getApplicationContext(),Welcome.class);
                    startActivity(i);
        break;
     }

在赋值之前,您不会从sharedprefs获取值。您没有为“协议”变量赋值,如何从中获取值?就是这样,

@Override
public void onClick(View v)
{
    switch (v.getId())
    {
    case R.id.button1:
        SharedPreferences settings = getSharedPreferences(filename, 0);
        SharedPreferences.Editor editor = settings.edit();
        editor.putBoolean("agreement", true);
        editor.commit();

        //now you can get value of agreement
    boolean agreement = settings.getBoolean("agreement",false);


        Intent i=new Intent(getApplicationContext(),Welcome.class);
                    startActivity(i);
        break;

         case R.id.button2:
             finish();
             System.exit(0);
         }
     }
}

那么现在发生了什么?为什么要使用getApplicationContext()启动活动?您遇到了什么错误?你说你得到了错误,但没有说它是什么。错误出现在第一个方法case R.id.button1的onClick中:这种情况下缺少一些代码,不知道如何解决它!是的,我刚刚通过这行在清单中声明了它:然后它也显示了相同的错误错误错误:10-13 07:17:35.639:E/AndroidRuntime(3313):android.content.ActivityNotFoundException:找不到显式活动类{com.example.disclaimer1/com.example.disclaimer1.Welcome};您是否在AndroidManifest.xml中声明了此活动?10-13 07:17:35.639:E/AndroidRuntime(3313):在dalvik.system.NativeStart.main(本机方法)中还包含完整的包名'ya它在正确的目录中,我在工作空间中检查了它两个java文件都在src/com/example/disclaimer1ok中,该应用程序运行平稳,但我希望在单击“同意”按钮后,我返回并再次打开免责声明1!应该只有欢迎屏幕应用程序运行顺利,但我希望在单击同意按钮后,我返回并再次打开免责声明1!应该只有欢迎屏幕
@Override
public void onClick(View v)
{
    switch (v.getId())
    {
    case R.id.button1:
        SharedPreferences settings = getSharedPreferences(filename, 0);
        SharedPreferences.Editor editor = settings.edit();
        editor.putBoolean("agreement", true);
        editor.commit();

        //now you can get value of agreement
    boolean agreement = settings.getBoolean("agreement",false);


        Intent i=new Intent(getApplicationContext(),Welcome.class);
                    startActivity(i);
        break;

         case R.id.button2:
             finish();
             System.exit(0);
         }
     }
}