无法在onClick中启动Intent活动



新方法中的startactivity()没有被调用,我尝试在onclick()方法中,直到i.p textra()方法它执行完美

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

webview类代码如下…

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

代码的日志如下:这是由我创建的编辑日志…

 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

你能像下面这样做吗?

getActivity().startActivity(i);

尝试使用getActivity().getBaseContext()

   Intent i = new Intent(getActivity().getBaseContext(), webView.class);
     startActivity(i);

您想尝试将Context传递给newMethod,即:

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

或者,您可以尝试将对话框放入新的Runnable()


根据您的评论更新:

这个问题可以用于"android.app.Fragment"的getActivity()或"android.support.v4.app.Fragment"

如果您使用的是"android.support.v4.app"。片段",你需要复习如果你没有使用getActivity从android.app。残片或恶习versa .

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

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

你这一行的错误

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

最新更新