本文介绍了得到错误操纵共享preferences?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想,该声明应在安装我的应用程序后,获得第一名的时间!
对于我MainActivity我有做2个按钮:第一个同意按钮,将存储布尔真淡水河谷在共享preference&安培;将其重定向到u在Welcome.class!而第二个按钮,将终止该应用程序!
在安装应用程序后,这种情况应该在第一时间!
我无法找到我的错误!?!

I want that the disclaimer should come 1st time after installing my app !For that in my MainActivity I have make 2 buttons : 1st 'agree' button will store boolean 'true' vale in the Shared Preference & will redirect it to u in the Welcome.class ! And the second button will terminate the app !This condition should come the first time after installing the app !I am not able to find my mistake !?!

MainActivity.java code:

MainActivity.java code:

    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中code:

activity_main.xml code :

   <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.java code:

Welcome.java code :

    public class Welcome extends Activity
    {
      @Override
     protected void onCreate(Bundle savedInstanceState)
     {
         super.onCreate(savedInstanceState);
                 setContentView(R.layout.welcome);
             TextView iv = (TextView)findViewById(R.id.textView1);

     }
}

的welcome.xml

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中code:

Here is the AndroidManifest.xml code :

<?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>

logcat的:

LOGCAT :

7月10日至13日:23:30.069:E / AndroidRuntime(3356):致命异常:主要
7月10日至13日:23:30.069:E / AndroidRuntime(3356):android.content.ActivityNotFoundException:无法找到明确的活动类{com.example.disclaimer1 / com.example.disclaimer1.Welcome};有你宣布你的Andr​​oidManifest.xml这个活动?
7月10日至13日:23:30.069:E / AndroidRuntime(3356):在android.app.Instrumentation.checkStartActivityResult(Instrumentation.java:1628)
7月10日至13日:23:30.069:E / AndroidRuntime(3356):在android.app.Instrumentation.execStartActivity(Instrumentation.java:1424)
7月10日至13日:23:30.069:E / AndroidRuntime(3356):在android.app.Activity.startActivityForResult(Activity.java:3390)
7月10日至13日:23:30.069:E / AndroidRuntime(3356):在android.app.Activity.startActivityForResult(Activity.java:3351)
7月10日至13日:23:30.069:E / AndroidRuntime(3356):在android.app.Activity.startActivity(Activity.java:3587)
7月10日至13日:23:30.069:E / AndroidRuntime(3356):在android.app.Activity.startActivity(Activity.java:3555)
7月10日至13日:23:30.069:E / AndroidRuntime(3356):在com.example.disclaimer1.MainActivity.onClick(MainActivity.java:54)
7月10日至13日:23:30.069:E / AndroidRuntime(3356):在android.view.View.performClick(View.java:4240)
7月10日至13日:23:30.069:E / AndroidRuntime(3356):在android.view.View $ PerformClick.run(View.java:17721)
7月10日至13日:23:30.069:E / AndroidRuntime(3356):在android.os.Handler.handleCallback(Handler.java:730)
7月10日至13日:23:30.069:E / AndroidRuntime(3356):在android.os.Handler.dispatchMessage(Handler.java:92)
7月10日至13日:23:30.069:E / AndroidRuntime(3356):在android.os.Looper.loop(Looper.java:137)
7月10日至13日:23:30.069:E / AndroidRuntime(3356):在android.app.ActivityThread.main(ActivityThread.java:5103)
7月10日至13日:23:30.069:E / AndroidRuntime(3356):在java.lang.reflect.Method.invokeNative(本机方法)
7月10日至13日:23:30.069:E / AndroidRuntime(3356):在java.lang.reflect.Method.invoke(Method.java:525)
7月10日至13日:23:30.069:E / AndroidRuntime(3356):在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:737)
7月10日至13日:23:30.069:E / AndroidRuntime(3356):在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
7月10日至13日:23:30.069:E / AndroidRuntime(3356):在dalvik.system.NativeStart.main(本机方法)

10-13 07:23:30.069: E/AndroidRuntime(3356): FATAL EXCEPTION: main10-13 07:23:30.069: E/AndroidRuntime(3356): android.content.ActivityNotFoundException: Unable to find explicit activity class {com.example.disclaimer1/com.example.disclaimer1.Welcome}; have you declared this activity in your AndroidManifest.xml?10-13 07:23:30.069: E/AndroidRuntime(3356): at android.app.Instrumentation.checkStartActivityResult(Instrumentation.java:1628)10-13 07:23:30.069: E/AndroidRuntime(3356): at android.app.Instrumentation.execStartActivity(Instrumentation.java:1424)10-13 07:23:30.069: E/AndroidRuntime(3356): at android.app.Activity.startActivityForResult(Activity.java:3390)10-13 07:23:30.069: E/AndroidRuntime(3356): at android.app.Activity.startActivityForResult(Activity.java:3351)10-13 07:23:30.069: E/AndroidRuntime(3356): at android.app.Activity.startActivity(Activity.java:3587)10-13 07:23:30.069: E/AndroidRuntime(3356): at android.app.Activity.startActivity(Activity.java:3555)10-13 07:23:30.069: E/AndroidRuntime(3356): at com.example.disclaimer1.MainActivity.onClick(MainActivity.java:54)10-13 07:23:30.069: E/AndroidRuntime(3356): at android.view.View.performClick(View.java:4240)10-13 07:23:30.069: E/AndroidRuntime(3356): at android.view.View$PerformClick.run(View.java:17721)10-13 07:23:30.069: E/AndroidRuntime(3356): at android.os.Handler.handleCallback(Handler.java:730)10-13 07:23:30.069: E/AndroidRuntime(3356): at android.os.Handler.dispatchMessage(Handler.java:92)10-13 07:23:30.069: E/AndroidRuntime(3356): at android.os.Looper.loop(Looper.java:137)10-13 07:23:30.069: E/AndroidRuntime(3356): at android.app.ActivityThread.main(ActivityThread.java:5103)10-13 07:23:30.069: E/AndroidRuntime(3356): at java.lang.reflect.Method.invokeNative(Native Method)10-13 07:23:30.069: E/AndroidRuntime(3356): at java.lang.reflect.Method.invoke(Method.java:525)10-13 07:23:30.069: E/AndroidRuntime(3356): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)10-13 07:23:30.069: E/AndroidRuntime(3356): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)10-13 07:23:30.069: E/AndroidRuntime(3356): at dalvik.system.NativeStart.main(Native Method)

推荐答案

请确保您在的Andr​​oidManifest.xml 文件中声明您的欢迎活动。

Make sure you declare your Welcome activity in your AndroidManifest.xml file.

最终答案

好吧,这不是最优雅的方式来做到这一点,但它会获得亚开始。后来,我建议你研究好了/长文本取消对话框,并使用那些不是有两个活动之一。

Ok, this isn't the most elegant way to do this, but it'll get ya started. Afterward, I'd suggest you investigate Ok/Cancel dialogs with long text, and use one of those instead of having two activities.

MainActivity

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();
    }
}

这篇关于得到错误操纵共享preferences?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-29 01:14