无法在onClick

时间:2015-09-26 13:50:12

标签: android android-intent fragment start-activity

新方法中的startactivity()没有被调用..我在onclick()方法中尝试过... i.putextra()方法它正在执行完美

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

代码的Logcat如下:这个由我创建的编辑日志...

 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

4 个答案:

答案 0 :(得分:1)

你可以这样做吗:

getActivity().startActivity(i);

希望这会对你有所帮助。

答案 1 :(得分:0)

尝试使用getActivity().getBaseContext()

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

答案 2 :(得分:0)

我想尝试将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);
   }
}

或者,您可以尝试将对话框放在https://www.rabbitmq.com/tutorials/tutorial-one-python.html

根据您的评论进行更新

  

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

     

如果您使用的是“android.support.v4.app.Fragment”,则需要查看   如果您没有使用“android.app.Fragment”中的getActivity或者恶意   反之亦然。

SRC:new Runnable()

答案 3 :(得分:0)

试试这个,

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