1

I am still learning to code, but apps i code persistently can't work on 2 diferent devices. Can Eclipse (latest android sdk) be the error or i code wrong every single app? Its force close on 2 devices

 public class MainActivity extends Activity {
Button dugme = (Button) findViewById(R.id.dugme);
@Override
protected void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
 setContentView(R.layout.activity_main);
 dugme.setOnClickListener(new View.OnClickListener(){
@Override
 public void onClick(View v) {
 Cao();
 } 
 });
}
private void Cao(){
 Intent Cao = new Intent(this, Cao.class);
 startActivity(Cao);
}}

This is Cao class

 public class Cao extends Activity{
protected void onCreate(Bundle savedInstanceState){
 super.onCreate(savedInstanceState);
 setContentView(R.layout.cao);
}}

And Manifest

 <?xml version="1.0" encoding="utf-8"?>
 <manifest xmlns:android="http://schemas.android.com/apk/res/android"
 package="com.example.book1"
 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.book1.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.book1.Cao"
 android:label="@string/app_name" >
 <intent-filter>
 <action android:name="android.intent.Action.VIEW" />
 <category android:name="android.intent.category.DEFAULT" />
 </intent-filter>
 </activity>
 </application></manifest>
asked Apr 9, 2013 at 12:53
3
  • Force close on 2 devices Commented Apr 9, 2013 at 12:55
  • 1
    A force close isn't an error, it's a problem. We need to know exactly how it crashed, not that it crashed Commented Apr 9, 2013 at 13:02
  • check persmission, clean project, restart eclipse..... Sometimes weird things happen in android world... Commented Apr 9, 2013 at 13:13

3 Answers 3

5

Try to put

Button dugme = (Button) findViewById(R.id.dugme);

this line in your onCreate method. A class shoud have only variables and methods. This line is definition. So I think that is only problem in your code. Other than there is nothing wrong in your code.

answered Apr 9, 2013 at 12:57
Sign up to request clarification or add additional context in comments.

1 Comment

that is the problem for sure. This line will be executed on construction, which means it won't find any ID and dugme will be null! Good catch!
1

Put this line

Button dugme = (Button) findViewById(R.id.dugme)

After

setContentView(R.layout.activity_main)

Android takes each reference of view_id according to its each layout xml

Think if android takes view reference_id before setting up layout to the given activity then we can not even keep same name of view ID for two different layout xml .

answered Apr 9, 2013 at 13:10

Comments

0

This line is wrong:

Button dugme = (Button) findViewById(R.id.dugme);

not because it not located in the onCreate method, but because it appears before this line:

 setContentView(R.layout.activity_main);

And the reason for that is: if you didn't set your layout as the content view of your activity you can't use the findViewById method, because there is no place that this view could be found (you layout is not yet set).

answered Apr 9, 2013 at 13:04

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.