开发者

Android Activity problem

Hi I need a little help here ...

thats the first part of my code :

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

    }); }

class Collections :

public class Collections extends Activity {

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


    Button boom = (Bu开发者_运维百科tton) findViewById(R.id.button01);
    setContentView(boom);
}

}

Main.xml

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

</LinearLayout>

and I've added in manifest file :

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

And the problem is that when I click the button on Mainactivity.Class the app is crashing.

Any idea what's the problem here?


You cant do this:

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

findViewById() searches for the id within the current content view, however you have not yet set a content view.


try this...

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

and again remove that intent filter from manifest..


The onCreate construction in Collections class is no acceptable. First, you need to inflate the layput before trying to inflate the button. You can not inflate a button and put it as content...

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜