0

I have created a simple app. one screen of my app has following code and I use a Button (id "sendManual") to change the app view to another one. please look at the end of this code, you can seen the button id "sendManual". I have setup it to change screen view to App2Activity.java

(I have put the Code of App2Activity java below this code, then you can check it also)

I installed this app on my android phone, But when I click Button(id"sendManual") app suddenly Stopped and says

Unfortunately,XXXXX app has stopped

public class showPermission implements View.OnClickListener {

Dialog dialog;
Activity activity;
public void showDialog(final Activity activity){
    dialog = new Dialog(activity);
    dialog.setCancelable(false);
    this.activity=activity;
    dialog.setTitle("XXXXX");
    dialog.setContentView(R.layout.permission);
    Button Sendcancel = (Button) dialog.findViewById(R.id.sendCancel);
    Button SendNow = (Button) dialog.findViewById(R.id.sendNow);
    Button sendManual = (Button) dialog.findViewById(R.id.sendManual);
    sendManual.setOnClickListener(this);
    SendNow.setOnClickListener(this);
    Sendcancel.setOnClickListener(this);
    dialog.show();
}


@Override
public void onClick(View view) {
    int id=view.getId();
    if(id==R.id.sendCancel){
        dialog.dismiss();
    }
    else if(id==R.id.sendNow){
        sendSMS();

    }
    else if(id==R.id.sendManual){
        Intent intent = new Intent(activity, App2Activity.class);
    activity.startActivity(intent); 
    }


}

Here is the code of App2Activity.java

public class App2Activity extends Activity {

Button button;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main2);

    findViewById(R.id.start).setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            sendSMS2();
        }
    });
}
............ etc

Here is manifest file.

<uses-permission android:name="android.permission.SEND_SMS"/>

<uses-sdk
    android:minSdkVersion="14"
    android:targetSdkVersion="16" />

<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <activity
        android:name=".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>
</application>

Cœur
  • 37,241
  • 25
  • 195
  • 267
Thunga
  • 63
  • 1
  • 9

2 Answers2

0

You are not properly set context in Intent.Use this

   Intent intent = new Intent(getApplicationContext(), App2Activity.class);
   startActivity(intent);

instead of

Intent intent = new Intent(activity, App2Activity.class);
activity.startActivity(intent); 
sasikumar
  • 12,540
  • 3
  • 28
  • 48
  • showPermission is a simple class not a component of android, so how you can use getApplicationContext() directly and startActivity without any context reference? @tunga is doing correct with activity references. – Ready Android Dec 10 '16 at 12:21
0

This all is happening because you didn't declare App2Activity in Manifest.xml. That is compulsory to declare all the activities in manifest file.

Replace your manifest.xml with this one:

<uses-sdk
    android:minSdkVersion="14"
    android:targetSdkVersion="16" />

<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <activity
        android:name=".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=".App2Activity"/>
</application>
Ready Android
  • 3,529
  • 2
  • 26
  • 40