2011-07-13 163 views
-1

您好我在這裏需要一些幫助......Android的活動問題

這就是我的代碼的第一部分:

public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.main); 

    Button coll = (Button) findViewById(R.id.collections); 
    coll.setOnClickListener(new View.OnClickListener() { 
     public void onClick(View view) { 
      Intent myIntent = new Intent(view.getContext(), Collections.class); 
      startActivity(myIntent); 
     } 

    }); } 

類類別:

public class Collections extends Activity { 

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


    Button boom = (Button) findViewById(R.id.button01); 
    setContentView(boom); 
} 

}

Main.xml

android:text="Boom" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
/> 

</LinearLayout> 

,我已經在清單文件中加入:

<activity android:name=".Collections"> 
    <intent-filter> 
      <action android:name="android.intent.action.MAIN" /> 
     </intent-filter> 
    </activity> 

而問題是,當我點擊Mainactivity.Class按鈕的應用程序崩潰。

任何想法這裏有什麼問題?

+0

從logcat顯示stracktrace(我假設您的清單中缺少Collections) – mibollma

+0

顯示您的logcat錯誤在您的問題 – Nikki

+0

[link] http://i304.photobucket.com/albums/nn170/damcrew /Capture.png [/ link] – hardartcore

回答

0

試試這個...

Intent myIntent = new Intent(this, Collections.class); 

再從清單中刪除這一意圖過濾器..

0

於類別類的onCreate建設沒有接受。首先,您需要在嘗試膨脹按鈕之前對產品充氣。你不能誇大一個按鈕,並把它作爲內容...

+0

public void onCreate(Bundle savedInstanceState){ super.onCreate(savedInstanceState); setContentView(R.layout.collections_layout); Button boom =(Button)findViewById(R.id.button01); boom.setOnClickListener(新View.OnClickListener(){ 公共無效的onClick(視圖v){ 意圖意圖=新意圖(v.getContext(),MainActivity.class); startActivity(意向); } } ); } 我做到了,它仍然崩潰 – hardartcore

1

你不能做到這一點:

Button boom = (Button) findViewById(R.id.button01); 
setContentView(boom); 

findViewById()查找當前內容視圖中的ID,但是你還沒有設置內容視圖。

+0

我做到了。我設置了setContentView(R.layout.collection_layout); 它仍然無法正常工作。 – hardartcore

+0

打開您的LogCat以查看導致錯誤的原因。它確實可以是任何事情。 – Cesar

+0

我解決了這個問題......這讓我問另一個問題......有什麼辦法可以做到這一點: 意圖myIntent = new Intent(view.getContext(),com.example.test.Collections.class); 或者我該如何創建一個Intent到一個不同包中的類? – hardartcore