0

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 :-

  1. menu.java
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);
          }
      } );

  }



}
  1. Modul_vokal.java

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

}
  1. 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?..

Boris Mocialov
  • 3,439
  • 2
  • 28
  • 55

3 Answers3

1

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.

Miro Markaravanes
  • 3,285
  • 25
  • 32
  • I have some problem to understand your answer – Blackbelt Jun 28 '13 at 12:38
  • @blackbelt feel free to ask any question you have. I'm here to answer you. – Miro Markaravanes Jun 28 '13 at 12:39
  • Here are some resources for information about contexts: http://stackoverflow.com/questions/3918083/what-exactly-is-a-context-in-java http://stackoverflow.com/questions/3572463/what-is-context-in-android http://bountyforu.blogspot.in/2013/06/what-is-context-in-android.html – Miro Markaravanes Jun 28 '13 at 12:44
  • neither of the links you provide talk about "menu context" – Blackbelt Jun 28 '13 at 12:45
  • I missed something in my answer. I'm correcting it now. Thanks for pointing out @blackbelt – Miro Markaravanes Jun 28 '13 at 12:46
  • @blackbelt I'm sorry. I thought it's something else. I will edit my answer to be correct. Thx – Miro Markaravanes Jun 28 '13 at 12:46
  • This still isn't the problem. `v.getContext()` returns the `Context` on which the `View` is running. In this case, it is the `Activity Context` which is what should be used to start a new `Activity` – codeMagic Jun 28 '13 at 12:53
  • @codeMagic well in this case it doesn't work. I never had problems with using `getApplicationContext()`. – Miro Markaravanes Jun 28 '13 at 12:58
  • Why doesn't it work in this case? How do you know that's the problem? I didn't say that `getApplicationContext()` won't work – codeMagic Jun 28 '13 at 12:59
  • @codeMagic Well that's why all the answers on the SO are not getting accepted. It's just a try and fail. People post their suggestions. It may or may not solve the issue. We are not magicians and we don't do magic. If I was on the poster's computer I could solve it. But until we're here we only guess. right? – Miro Markaravanes Jun 28 '13 at 13:13
  • Easy friend. I was just pointing out why this doesn't solve it and why the `context` used should be fine. We can't know the problem without seeing the logcat. I have used `v.getContext()` and it works fine. What I wrote about the `Context` returned is from the Docs – codeMagic Jun 28 '13 at 13:15
  • @codeMagic I'm easy :) I read the docs too and corrected my answer. I don't say you're wrong. – Miro Markaravanes Jun 28 '13 at 13:25
  • Good, now we can have a friendly debate :) My point is that the `Context` returned by the `View` is the `Activity Context` which is generally used to start an `Activity` as in `MainActivity.this`. I'm not saying you're wrong either I just disagree which is why I deleted my similar answer – codeMagic Jun 28 '13 at 13:29
  • Well I'm not sure :) I'm not too professional in Android because I started it, Two-three months ago :) I just thought that it might be the problem. Turns out it wasn't :) – Miro Markaravanes Jun 28 '13 at 13:35
  • And that's fine, I was just giving you my opinion on it. That's why we are all here...to learn. I'm not claiming to have all the answers either but just adding my input so it may help – codeMagic Jun 28 '13 at 13:45
0

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.

DuffMck
  • 81
  • 7
0

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.

jigar
  • 1,571
  • 6
  • 23
  • 46