2012-08-27 54 views
0

我有一個問題,我很困惑。我有一個對話框,它包含一個按鈕。 我表現出的主要活動的對話框與此代碼,就這麼簡單:從android sdk 11或更高版本的自定義對話框啓動活動

InfoDialog id=new InfoDialog(this); 
id.show(); 

,我希望當我點擊按鈕,它會啓動第二個活動。你看它很容易,我用這個代碼:

public class InfoDialog extends Dialog implements android.view.View.OnClickListener{ 

    Context context; 

    public InfoDialog(Context context) { 
     super(context); 
     setContentView(R.layout.infodialog); 
     this.context=context; 
     setupViews(); 
    } 

    private void setupViews() { 
     Button button=(Button)findViewById(R.id.button1); 
     button.setOnClickListener(this);  
    } 

    @Override 
    public void onClick(View v) { 

     switch (v.getId()){ 
     case R.id.button1: 
      Intent newintent= new Intent(context,DestActivity.class); 
      context.startActivity(newintent); 
      break; 
    } 
} 

我也在聲明destAct。當我在Gingerbread Android上運行它時,它可以正常工作。但是當我在冰淇淋三明治(sdk 11)或更高版本上運行此代碼時,它會讓我逼近!

這是我的日誌貓:

08-27 09:38:54.540: W/dalvikvm(10945): threadid=1: thread exiting with uncaught exception (group=0x40a3d1f8) 
08-27 09:38:54.544: E/AndroidRuntime(10945): FATAL EXCEPTION: main 
08-27 09:38:54.544: E/AndroidRuntime(10945): java.lang.NullPointerException 
08-27 09:38:54.544: E/AndroidRuntime(10945): at my.app.InfoDialog.onClick(InfoDialog.java:131) 
08-27 09:38:54.544: E/AndroidRuntime(10945): at android.view.View.performClick(View.java:3511) 
08-27 09:38:54.544: E/AndroidRuntime(10945): at android.view.View$PerformClick.run(View.java:14105) 
08-27 09:38:54.544: E/AndroidRuntime(10945): at android.os.Handler.handleCallback(Handler.java:605) 
08-27 09:38:54.544: E/AndroidRuntime(10945): at android.os.Handler.dispatchMessage(Handler.java:92) 
08-27 09:38:54.544: E/AndroidRuntime(10945): at android.os.Looper.loop(Looper.java:137) 
08-27 09:38:54.544: E/AndroidRuntime(10945): at android.app.ActivityThread.main(ActivityThread.java:4575) 
08-27 09:38:54.544: E/AndroidRuntime(10945): at java.lang.reflect.Method.invokeNative(Native Method) 
08-27 09:38:54.544: E/AndroidRuntime(10945): at java.lang.reflect.Method.invoke(Method.java:511) 
08-27 09:38:54.544: E/AndroidRuntime(10945): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:789) 
08-27 09:38:54.544: E/AndroidRuntime(10945): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:556) 
08-27 09:38:54.544: E/AndroidRuntime(10945): at dalvik.system.NativeStart.main(Native Method) 

到底是什麼呢?請幫幫我

+0

請澄清一下:「我點擊按鈕第二次開始第二個活動」,並告訴我們您的logcat結果。 – Mohit

+1

發佈您的logcat。 – Sri

+0

@Deadlock。張貼編輯我的朋友。 tnx您的評論 – aTa

回答

0

終於我解決它。我使用了一個處理程序,並在處理程序消息接收上寫入startActivity代碼。並在按鈕的onClick方法,只是我發送消息給我的處理程序。它可以在sdk> 11上正常工作。但我現在還沒有爲什麼我的舊代碼沒有工作;)

0

主要問題在於聲明你的Button,這不是你如何給你的對話框中的按鈕留下記憶,你應該首先獲得父對象的引用(即對話框在這裏),然後你應該找到視圖

爲了您更好地理解我在下面的方式重建你的類,請試試這個,讓我知道

 Context context; 
View parentView; // THIS IS ADDED 

public InfoDialog(Context context) { 
    super(context); 
    parentView=setContentView(R.layout.infodialog); //UPDATED 
    this.context=context; 
    setupViews(); 
} 


private void setupViews() { 
    Button button=(Button)parentView.findViewById(R.id.button1); //UPDATED 
    button.setOnClickListener(this);  
} 
+0

修改後,你不會得到一個空指針異常(因爲你無法找到它的正確引用它得到了空指針) – Anuj

0

這裏有一個更簡單的溶膠:

而且我沒不要用th Ëcontext.startActivity();)

public class MainActivity extends Activity { 

final Context context = this; 
private Button button; 

public void onCreate(Bundle savedInstanceState) { 

    super.onCreate(savedInstanceState); 
    setContentView(R.layout.main); 

    button = (Button) findViewById(R.id.buttonShowCustomDialog); 

    // add button listener 
    button.setOnClickListener(new OnClickListener() { 

     @Override 
     public void onClick(View arg0) { 

      // custom dialog 
      final Dialog dialog = new Dialog(context); 
      dialog.setContentView(R.layout.custom); 
      dialog.setTitle("Some Title"); 

      // set the custom dialog components - text, image and button 
      TextView text = (TextView) dialog.findViewById(R.id.text); 
      text.setText("custom dialog"); 
      ImageView image = (ImageView) dialog.findViewById(R.id.image); 
      image.setImageResource(R.drawable.ic_launcher); 

      Button dialogButton = (Button) dialog.findViewById(R.id.dialogButtonOK); 
      // if button is clicked, close the custom dialog 
      dialogButton.setOnClickListener(new OnClickListener() { 
       @Override 
       public void onClick(View v) { 
        switch (v.getId()){ 
        case R.id.dialogButtonOK: 
         Intent newintent= new Intent(context,OtherActivity.class); 
         startActivity(newintent); 
         break; 
        } 
       } 
      }); 

      dialog.show(); 
     } 

    }); 

} 

}

這裏是我的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" > 

    <ImageView 
     android:id="@+id/image" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_marginRight="5dp" /> 

    <TextView 
    android:id="@+id/text" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:textColor="#FFF" 
    android:layout_toRightOf="@+id/image"/> 

<Button 
    android:id="@+id/dialogButtonOK" 
    android:layout_width="100px" 
    android:layout_height="wrap_content" 
    android:text=" Ok " 
    android:layout_marginTop="5dp" 
    android:layout_marginRight="5dp" 
    android:layout_below="@+id/image" 
    /> 

    </RelativeLayout> 

而且在OtherActivity我剛剛出現舉杯即:

protected void onCreate(Bundle savedInstanceState) { 
    // TODO Auto-generated method stub 
    super.onCreate(savedInstanceState); 
    Toast.makeText(this, "ok working fine", Toast.LENGTH_LONG).show(); 

} 

嘗試這是非常整齊,乾淨和自我解釋的代碼。保持事情儘可能簡單,你可以...

死鎖...