应用程序需要在触摸按钮时打开一些网站。有2个标签(2个片段),带有不同的按钮(1个按钮= 1个链接) 当我单击需要打开WebView(打开链接)应用程序崩溃的按钮时。我认为,错误在AFragment.java中的某个地方
main.java(很好99.9%)
import android.app.ActionBar;
import android.app.ActionBar.Tab;
import android.app.Activity;
import android.app.Fragment;
import android.app.FragmentTransaction;
import android.content.Context;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.widget.Toast;
public class MainActivity extends Activity {
public static Context appContext;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
appContext = getApplicationContext();
//ActionBar
ActionBar actionbar = getActionBar();
actionbar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
ActionBar.Tab PTab = actionbar.newTab().setText("One");
ActionBar.Tab DTab = actionbar.newTab().setText("Two");
Fragment OneFragment = new AFragment();
Fragment TwpFragment = new BFragment();
PTab.setTabListener(new MyTabsListener(OneFragment));
DTab.setTabListener(new MyTabsListener(TwoFragment));
actionbar.addTab(PTab);
actionbar.addTab(DTab);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.activity_main, menu);
return true;
}
@Override
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
outState.putInt("tab", getActionBar().getSelectedNavigationIndex());
}
}
class MyTabsListener implements ActionBar.TabListener {
public Fragment fragment;
public MyTabsListener(Fragment fragment) {
this.fragment = fragment;
}
@Override
public void onTabReselected(Tab tab, FragmentTransaction ft) {
//Toast.makeText(MainActivity.appContext, "Reselected!", Toast.LENGTH_LONG).show();
}
@Override
public void onTabSelected(Tab tab, FragmentTransaction ft) {
ft.replace(R.id.fragment_container, fragment);
}
@Override
public void onTabUnselected(Tab tab, FragmentTransaction ft) {
ft.remove(fragment);
}
}
AFragment.java:
import android.app.Fragment;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.WebView;
import android.widget.Button;
import android.widget.EditText;
public class AFragment extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle
SavedInstanceState) {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.afragment, container, false);
}
// On Button Click
public void openIt(View view) {
Intent intent = new Intent(view.getContext(), openPage.class);
startActivity(intent);
}
}
openPage.class:
import android.webkit.WebView;
import android.app.Activity;
import android.os.Bundle;
public class openPage extends Activity {
WebView myWebView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.webview);
WebView myWebView = (WebView) findViewById(R.id.webview);
myWebView.loadUrl("http://safhkalfsd.com");
}
}
布局很好99,99% 我放 安卓的onClick = “openIt” 在Button里面的afragment.xml中 谢谢你
答案 0 :(得分:0)
解决了! 我需要说:
// On Button Click
public void openIt(View view) {
Intent intent = new Intent(view.getContext(), openPage.class);
startActivity(intent);
}
在MainActivity中,而不是'AFragment'。