0

The startactivity() inside the new method is not getting called..i tried inside onclick() method..till i.putextra() method it is executing perfectly

public class First_Fragment extends Fragment{

View myView;
EditText figText;
Button figButton;
String TAG ="com.myapplication.siva.navigation_drawer";

@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {

    myView = inflater.inflate(R.layout.first_layout,container,false);
    figText= (EditText) myView.findViewById(R.id.figText);
    figButton= (Button) myView.findViewById(R.id.figButton);
    Log.i(TAG,"Going inside 1");
    figButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            newMethod();
        }
    });

    return myView;
}


 public void newMethod()
  {
    Log.i(TAG,"Going inside 2");
    String id=figText.getText().toString();
    Log.i(TAG,"Going inside 3");
    Intent i = new Intent(getActivity(), webView.class);
    Log.i(TAG,"Going inside 4");
    i.putExtra("sivMessage",id);
    Log.i(TAG,"Going inside 5");
     startActivity(i);
   }
}

the webview class code is as follows..

import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.View;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.widget.TextView;


public class webView extends ActionBarActivity {

WebView figWeb;
String TAG ="com.myapplication.siva.sasfig";
long num;
String abc;
TextView regNo;

public webView() {
}


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.web_view);
    getSupportActionBar().hide();

    regNo=(TextView)findViewById(R.id.regNo);
    figWeb = ((WebView) findViewById(R.id.figWeb));

    WebSettings webSettings = figWeb.getSettings();
    webSettings.setBuiltInZoomControls(true);
    webSettings.setSupportZoom(true);
    webSettings.setJavaScriptEnabled(true);
    Bundle man = getIntent().getExtras();
    if(man == null){
        return;
    }
    String sivMessage = man.getString("sivMessage");
    num=Long.parseLong(sivMessage);
    regNo.setText(sivMessage);
    figWeb.loadUrl("http://192.6.18/memberaccess1.asp?id="+num);
}
public void onPlus(View view)
{
    num+=1;
    abc=""+num;
    regNo.setText(abc);
    figWeb.loadUrl("http://192.6.18/memberaccess1.asp?id="+num);
}
public void onMinus(View view)
{
    num-=1;
    abc=""+num;
    regNo.setText(abc);
    figWeb.loadUrl("http://192.6.18/memberaccess1.asp?id="+num);
}
}

Logcat for the code is given below:this isedited log created by me...

 09-26 16:51:43.984  17879-17879/com.myapplication.siva.navigation_drawer I/com.myapplication.siva.navigation_drawer﹕ Going inside 1
 09-26 16:51:43.984  17879-17879/com.myapplication.siva.navigation_drawer I/com.myapplication.siva.navigation_drawer﹕ Going inside 2
 09-26 16:51:43.984  17879-17879/com.myapplication.siva.navigation_drawer I/com.myapplication.siva.navigation_drawer﹕ Going inside 3
 09-26 16:51:43.991  17879-17879/com.myapplication.siva.navigation_drawer I/com.myapplication.siva.navigation_drawer﹕ Going inside 4
Siva
  • 57
  • 1
  • 1
  • 7
  • What is logcat saying? – Pedro Lobito Sep 26 '15 at 14:04
  • Going inside 2 Going inside 3 Going inside 4 Going inside 5 after that the app gets closed – Siva Sep 26 '15 at 14:07
  • as you said app gets closed...possiblity of error so post logcat report along with second Activity – Shadow Droid Sep 26 '15 at 14:11
  • As mentioned by others *POST YOUR LOGCAT*. Also I suspect the problem is in your `webview` code and not in the code you've posted - post the code for `webview` as well as the logcat stacktrace. One last thing - starting an `Activity` from a `Fragment` isn't really good coding practice - the `Activity` holding the `Fragment` should be responsible for starting any new `Activities`. – Squonk Sep 26 '15 at 14:19
  • have you try with _this_ or _First_Fragment.this_ ? – Davide Sep 26 '15 at 14:37
  • @Squonk i uploaded it... – Siva Sep 26 '15 at 18:55
  • @Siva : The logcat extract you posted shows nothing but the informal locg messages. If the app is being closed ther will be a stacktrace showing an unhandled exception - find that in logcat and post it here. – Squonk Sep 27 '15 at 10:20

4 Answers4

1

Can you please do like below:

getActivity().startActivity(i);

Hope this will help you.

Hiren Patel
  • 52,124
  • 21
  • 173
  • 151
0

Try using getActivity().getBaseContext()

   Intent i = new Intent(getActivity().getBaseContext(), webView.class);
     startActivity(i);
Rajan Kali
  • 12,627
  • 3
  • 25
  • 37
0

You my want to try passing the Context to newMethod, i.e.:

    public void onClick(View v) {
        newMethod(getActivy());
    }

 public void newMethod(Context ctx)
  {
    Log.i(TAG,"Going inside 2");
    String id=figText.getText().toString();
    Log.i(TAG,"Going inside 3");
    Intent i = new Intent(ctx, webView.class);
    Log.i(TAG,"Going inside 4");
    i.putExtra("sivMessage",id);
    Log.i(TAG,"Going inside 5");
     startActivity(i);
   }
}

Alternatively, you can try to place the dialog inside a new Runnable()


Update Based on your comment:

This problem can be for used getActivity() of "android.app.Fragment" or "android.support.v4.app.Fragment"

if your are using "android.support.v4.app.Fragment" you need to review if you aren't using getActivity from "android.app.Fragment" or vice versa.

SRC: https://stackoverflow.com/a/15908255/797495

Community
  • 1
  • 1
Pedro Lobito
  • 94,083
  • 31
  • 258
  • 268
0

try this,

public void newMethod()
      {
        Log.i(TAG,"Going inside 2");
        String id=figText.getText().toString();
        Log.i(TAG,"Going inside 3");

// The following line is edited.

        Intent i = new Intent(getApplicationContext(), webView.class);

        Log.i(TAG,"Going inside 4");
        i.putExtra("sivMessage",id);
        Log.i(TAG,"Going inside 5");
         startActivity(i);
       }

your mistake in this line

Intent i = new Intent(getApplicationContext(), webView.class);
Ganpat Kaliya
  • 888
  • 2
  • 9
  • 16