Reputation: 9
i have created apps in eclipse with target sdk in API 15 and. I want to make button start new activity. i have created that code but i have a problem. when i run in on my phone ninetology stealh 2, when i click a button and my app has crash and my phone show me this :
"Unfortunately start"
i have tested it in others phone two and it show same problem. i also use more technique that i learn from thenewboston, vongella an other but still have same problem. here is my .xml and java code :-
package com.mytuturkiu.mytuturkiu; import android.app.Activity; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.content.Intent; public class Menu extends Activity { Button btnmodul; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_menu); btnmodul = (Button)findViewById(R.id.btnmodul); btnmodul.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub Intent vokal = new Intent(v.getContext(),Modul_Vokal.class); startActivity(vokal); } } ); } }
package com.mytuturkiu.mytuturkiu;
import android.os.Bundle; import android.app.Activity; import android.view.Menu; public class Modul_Vokal extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_modul__vokal); } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.modul__vokal, menu); return true; } }
AndroidManifest.xml
<uses-sdk
android:minSdkVersion="14"
android:targetSdkVersion="15" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.mytuturkiu.mytuturkiu.Menu"
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.mytuturkiu.mytuturkiu.Modul_Vokal"
android:label="@string/title_activity_modul__vokal" >
</activity>
</application>
what problem with my apps?..
Upvotes: 0
Views: 125
Reputation: 1591
Try,
Intent vokal = new Intent(getApplicationContext(),Modul_Vokal.class);
startActivity(vokal);
in place of,
Intent vokal = new Intent(v.getContext(),Modul_Vokal.class);
startActivity(vokal);
It will definately work my friend.
Upvotes: 0
Reputation: 81
You can add a function like this in your class:
public class Menu extends Activity {
Button btnmodul;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_menu);
btnmodul = (Button)findViewById(R.id.btnmodul);
btnmodul.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// CALL THE FUNCTION
startMyActivity();
}
} );
}
private void startMyActivity(){
Intent vokal = new Intent(this,Modul_Vokal.class);
startActivity(vokal);
}
}
The function is in your activity context and when you put "this" in the Intent constructor, it will take the correct one.
Upvotes: 0
Reputation: 3367
You should not do v.getContext()
... You should use getApplicationContext()
..
The View v
is the view you clicked on. So v.getContext()
returns the context which the view runs on. This is the issue.
I think using Application's context is better and doesn't cause an issue.
Upvotes: 1