-2

I want to show second activity (Form). I was used the this code to show second activity.

// Code

Intent intent=new Intent(v.getContext(),MainActivity2Activity.class);
startActivityForResult(intent,0);

This code is working fine if i did not add my method. when i click button to show second activity or first activity it is working fine, but the problem when i add my method i got the error say "Unfortunately App Name has stopped".

//My code of first activity are.

public class MainActivity extends ActionBarActivity implements View.OnClickListener {
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    Button btn1=(Button)findViewById(R.id.btnsignup);
    btn1.setOnClickListener(this);

}

@Override
public void onClick(View v) {
    Intent intent=new Intent(v.getContext(),MainActivity2Activity.class);
    startActivityForResult(intent,0);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.menu_main, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();

    //noinspection SimplifiableIfStatement
    if (id == R.id.action_settings) {
        return true;
    }

    return super.onOptionsItemSelected(item);
}
}

// My second activity is

@SuppressWarnings("unused")
private static final String PHOTO_BASE_URL="";
TextView OutPut;
ProgressBar pg;
List<MyTask> tasks;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    Button btn1=(Button)findViewById(R.id.btnPost);

    btn1.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            startCall();
        }


    });

    OutPut=(TextView)findViewById(R.id.textView);
    OutPut.setMovementMethod(new ScrollingMovementMethod());

    pg =(ProgressBar)findViewById(R.id.progressBar);
    pg.setVisibility(View.INVISIBLE);

    tasks=new ArrayList<>();


}

private void startCall() {
   // if(isOnline()){
        requestData("http://10.0.2.2/android.php");
    //}else{
    //    Toast.makeText(this, "Network is not available", Toast.LENGTH_LONG).show();
    //}
}

private void requestData(String url) {
    // updateDisplay("Done it");
    RequestPackage p=new RequestPackage();
    p.setMethod("GET");
    //p.setMethod("POST");
    p.setUri(url);
    p.setParam("name1", "makame haji 1");
    p.setParam("name2","makame haji 2");
    p.setParam("name3","makame haji 3");

    MyTask task = new MyTask();
    task.execute(p);
}



protected boolean isOnline(){
    ConnectivityManager cm=(ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo netInfor = cm.getActiveNetworkInfo();
    if(netInfor != null && netInfor.isConnectedOrConnecting()){
        return true;
    }else{
        return false;
    }
}

private  class  MyTask extends AsyncTask<RequestPackage,String,String>{

   @Override
    protected void onPreExecute(){
       // updateDisplay("Started it");
        if(tasks.size()==0){
            pg.setVisibility(View.VISIBLE);
        }
        tasks.add(this);
    }

    @Override
    protected  String doInBackground(RequestPackage... params){
        String contect = HttpManager.getData(params[0]);
        // use this for pawword
        //String contect = HttpManager.getData_By_PASSWORD(params[0],"username","password");
        /*flowersList=FlowerJSONParser.parseFeed(contect);

        for(Flowers flowers:flowersList){
            try{
                String imageUrl=PHOTO_BASE_URL+flowers.getBitmap();
                InputStream in=(InputStream) new URL(imageUrl).getContent();
                Bitmap bitmap= BitmapFactory.decodeStream(in);
                flowers.setBitmap(bitmap);
                in.close();
            }catch (Exception e){
                e.printStackTrace();
            }
        }*/
        return  contect;
    }

   @Override
    protected  void  onPostExecute(String result){
       //flowersList= FlowerJSONParser.parseFeed(result);
       //flowersList= persers.parseFeed(result);
        updateDisplay(result);

       tasks.remove(this);
       if(tasks.size()==0){
            pg.setVisibility(View.INVISIBLE);
        }
    }

   @Override
    protected  void onProgressUpdate(String... values){
        //updateDisplay(values[0]);
    }
}

}

Please any one help me

Anirudh Sharma
  • 7,968
  • 13
  • 40
  • 42
Makame Sharif
  • 65
  • 1
  • 1
  • 8
  • do you have declare the second activity in your manifest? – br00 Aug 06 '15 at 13:30
  • possible duplicate of [Unfortunately MyApp has stopped. How can I solve this?](http://stackoverflow.com/questions/23353173/unfortunately-myapp-has-stopped-how-can-i-solve-this) – Selvin Aug 06 '15 at 13:30
  • instead of v.getContext() use MainActivity.this – karan Aug 06 '15 at 13:30
  • @KaranMer ... you just achive [permutation master achivment](https://en.wikipedia.org/wiki/Programming_by_permutation) ... congratz...(yeah, it is an irony coz it doesn't really matter ... as v.getContext() == MainActivity.this or is a ContextWrapper around MainActivity.this) – Selvin Aug 06 '15 at 13:31
  • look here http://developer.android.com/training/basics/firstapp/starting-activity.html – br00 Aug 06 '15 at 13:31
  • @Selvin ... lol I take it as complement. – karan Aug 06 '15 at 13:33
  • complement? *This approach sometimes seems attractive when the programmer does not fully understand the code and believes that one or more small modifications may result in code that is correct.* ... – Selvin Aug 06 '15 at 13:35

2 Answers2

0

Check is internet permission added in Manifest:

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

Also please print full stacktrace of error

Beyka
  • 1,372
  • 1
  • 10
  • 15
0

Do you have any logs to give us ? Please.

You should use native stuff:

Button mButton = (Button) findViewById(R.id.your_button_id_in_the_layout);
Log.d(TAG, " onCreate");
mButton.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        Intent intent = new Intent(MainActivity.this, DummyActivity.class);
        startActivity(intent);
    }
});
jerome.v
  • 23
  • 5