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>
-
Force close on 2 devicesuser2109172– user21091722013年04月09日 12:55:27 +00:00Commented Apr 9, 2013 at 12:55
-
1A force close isn't an error, it's a problem. We need to know exactly how it crashed, not that it crashedMatt Taylor– Matt Taylor2013年04月09日 13:02:43 +00:00Commented Apr 9, 2013 at 13:02
-
check persmission, clean project, restart eclipse..... Sometimes weird things happen in android world...babeyh– babeyh2013年04月09日 13:13:32 +00:00Commented Apr 9, 2013 at 13:13
3 Answers 3
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.
1 Comment
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 .
Comments
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).