【问题标题】:Unable to start a Intent activity inside onClick无法在 onClick 中启动 Intent 活动
【发布时间】:2015-09-26 13:50:12
【问题描述】:

新方法中的 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

【问题讨论】:

  • logcat 在说什么?
  • 进入 2 进入 3 进入 4 进入 5 之后应用程序关闭
  • 正如您所说的应用程序已关闭...可能出现错误,因此将 logcat 报告与第二个活动一起发布
  • 正如其他人提到的那样发布您的 LOGCAT。此外,我怀疑问题出在您的 webview 代码中,而不是您发布的代码中 - 发布 webview 的代码以及 logcat 堆栈跟踪。最后一件事 - 从Fragment 开始Activity 并不是很好的编码实践 - 持有FragmentActivity 应该负责开始任何新的Activities
  • 你试过 thisFirst_Fragment.this 吗?

标签: android android-intent fragment start-activity


【解决方案1】:

你能做到以下几点:

getActivity().startActivity(i);

希望这会对你有所帮助。

【讨论】:

    【解决方案2】:

    尝试使用getActivity().getBaseContext()

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

    【讨论】:

    • 我看不到更新...您能具体说明您所做的更改...请
    【解决方案3】:

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

    或者,您可以尝试将对话框放在 new Runnable()


    根据您的评论更新

    这个问题可能是使用了“android.app.Fragment”的getActivity() 或“android.support.v4.app.Fragment”

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

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

    【讨论】:

    • 你可以试试getSupportActivity()
    【解决方案4】:

    试试这个,

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-12-10
      • 1970-01-01
      相关资源
      最近更新 更多