2013-10-13 72 views
0

我希望在安裝我的應用程序後首次發佈免責聲明! 對於在我的MainActivity中,我做了2個按鈕:第一個'同意'按鈕將存儲共享首選項&布爾'真'瓦爾將它重定向到你在Welcome.class!第二個按鈕將終止該應用程序! 這種情況應該在安裝應用程序後首次出現! 我無法找到我的錯誤!?!操作共享首選項時出錯?

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.java代碼:

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

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

logcat的:

七月10日至13日: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):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):在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):at android.app.Activity.startActivity(Activity.java:3555) 10-13 07:23: 30.069:E/AndroidRuntime(3356):at com.example.disclaimer1.MainActivity.onClick(Main Activity.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):在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):在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):在dalvik.system.NativeStart.main(本機方法)

+0

所以現在最新情況如何? –

+0

你爲什麼使用getApplicationContext()來開始活動? – Vivart

+0

您遇到的錯誤是什麼?你說你得到錯誤,但不要說它是什麼。 – Kuffs

回答

1

確保您在AndroidManifest.xml中文件聲明你歡迎活動。

最終答案

好吧,這是不是最優雅的方式來做到這一點,但它會獲得亞開始。之後,我建議你使用長文本調查確定/取消對話框,並使用其中一個而不是兩個活動。

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

雅我剛剛聲明它清單中的這一行: 然後它也顯示相同的錯誤 –

+0

錯誤:10-13 07:17:35.639:E/AndroidRuntime(3313):android.content.ActivityNotFoundException:無法找到顯式活動類{com.example.disclaimer1/com.example.disclaimer1.Welcome};你有沒有在你的AndroidManifest.xml中聲明這個活動? 7月10日至13日:17:35.639:E/AndroidRuntime(3313):\t在dalvik.system.NativeStart.main(本機方法) –

+0

還包括完整的包名」 –

0

它只是你首先要設置的值在`Sharedpreference再之後,纔可以訪問它。

嘗試如下:

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; 
    } 
+0

該應用程序運行平穩,但我希望點擊同意按鈕後,我回去再次打開disclaimer1!那裏只有歡迎屏幕 –

0

你之前不分配價值得到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); 
     } 
    } 
} 
+0

好的,該應用程序運行平穩,但我想單擊同意按鈕後,我回去,再次打開disclaimer1!應該只有歡迎屏幕 –