2012-04-27 7 views
1

ログインアクティビティを初めて実行するときは、最初と次回、2つの異なるアクティビティを開く必要がありますログインボタンをクリックします。 ログインアクティビティでは、次回に最初のラジオボタンを有効にすると2つのラジオボタンが表示され、次回はメイン2のレイアウトが表示され、2番目のラジオボタンを有効にするとmain3のレイアウトが表示されます。この例ではログインアクティビティを初めて取得して初めて次回アクティビティを実行したときに

 public class Demo1 extends Activity { 
     Button b1,b2; 
     int count=0; 
     public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 

     SharedPreferences settings = this.getSharedPreferences("MyApp",0); 
     boolean firstrun=settings.getBoolean("firstrun",true); 
     if (firstrun) { 
     SharedPreferences.Editor e = settings.edit(); 
     e.putBoolean("firstrun",false); 
     e.commit(); 


     setContentView(R.layout.main1); 
     } 
     else{ 
     test(); 
     } 
     b1=(Button)findViewById(R.id.button1); 
     b2=(Button)findViewById(R.id.button2); 
    b1.setOnClickListener(new OnClickListener() { 

     public void onClick(View v) { 
      // TODO Auto-generated method stub 
      count=1; 
      Intent intent = new Intent(Demo1.this, ButtonActivate1.class); 
       startActivity(intent); 
     } 
    }); 
    b2.setOnClickListener(new OnClickListener() { 

     @Override 
     public void onClick(View v) { 
      count=2; 
      // TODO Auto-generated method stub 
      Intent intent = new Intent(Demo1.this, ButtonActivate1.class); 
       startActivity(intent); 
     } 
    }); 




} 
private void test() { 
    // TODO Auto-generated method stub 
    if(count==1) 
    { 
     setContentView(R.layout.main2); 
    } 
    if(count==2) 
    { 
     setContentView(R.layout.main3); 
    } 
} 

}

私は力が閉じて取得し、ラインbutton.setonclicklistner.pleaseでエラーを示している実行していたときに、この問題を解決する方法を私に示唆しています。

+0

エラーは何ですか? – 5hssba

+0

ヌルポインタ例外 – user1083266

答えて

2

私は以下に私の答えを更新しました。これは今あなたのために働くはずです。

package com.demo1; 

import android.app.Activity; 
import android.content.SharedPreferences; 
import android.os.Bundle; 
import android.view.View; 

public class Demo1Activity extends Activity { 
    private int count; 
    private SharedPreferences settings; 


    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 

     settings = this.getSharedPreferences("MyApp",0); 
     count = settings.getInt("count", 0); 

     selectContentView(); 
    } 

    public void button1Method(View v) { 
     setCount(1); 
     Intent intent = new Intent(Demo1Activity.this, Demo2Activity.class); 
     startActivity(intent); 
    } 

    public void button2Method(View v) { 
     setCount(2); 
     Intent intent = new Intent(Demo1Activity.this, Demo3Activity.class); 
     startActivity(intent); 
    } 


    private void selectContentView() { 
     // TODO Auto-generated method stub 
     switch (count) { 
     case 1: 
      setContentView(R.layout.main2); 
      break; 
     case 2: 
      setContentView(R.layout.main3); 
      break; 
     default: 
      setContentView(R.layout.main1); 
     } 
    } 

    private void setCount(int count) { 
     SharedPreferences.Editor e = settings.edit(); 
     e.putInt("count",count); 
     e.commit(); 
    } 
} 

main1.xmlファイル:

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

    <TextView 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:text="main1" /> 

    <LinearLayout 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:layout_alignParentBottom="true" 
     android:orientation="horizontal" > 

     <Button 
      android:id="@+id/button1" 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:layout_weight="0.5" 
      android:onClick="button1Method" 
      android:text="button1" /> 

     <Button 
      android:id="@+id/button2" 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:layout_weight="0.5" 
      android:onClick="button2Method" 
      android:text="button2" /> 
    </LinearLayout> 

</RelativeLayout> 

を私は、XMLファイル内で設定するのonClickリスナを交換しました。私はこれをもっときれいに見つけた。

マニフェストファイル:

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

    <uses-sdk android:minSdkVersion="10" /> 

    <application 
     android:icon="@drawable/ic_launcher" 
     android:label="@string/app_name" > 
     <activity 
      android:name=".Demo1Activity" 
      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=".Demo2Activity" 
      android:label="@string/app_name" /> 
     <activity 
      android:name=".Demo3Activity" 
      android:label="@string/app_name" /> 
    </application> 

</manifest> 
+0

は、クリック時には最初にエラーが表示されていることを意味します。 – user1083266

+0

コードには多くの問題があります。 (i)クラスが初期化するときに 'int count = 0;'を定義しているので、アプリケーションの再起動時に常にcount = 0が得られます。だからテストは決してうまくいかないでしょう。それをもっと見てみましょう – ASceresini

+0

上記の例は機能します。それをテストしました!マニフェストファイル内で2番目と3番目のアクティビティが宣言されていることを忘れないでください。これは、アクティビティを開くためのインテントを呼び出し、マニフェストファイルで宣言していない場合に例外をスローします。 – ASceresini

関連する問題