0

当我触摸按钮时,应用程序需要打开一些网站。有 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% 我把 android:onClick="openIt" 放在 afragment.xml 里面的 Button THANK YOU

4

1 回答 1

0

解决了!我需要说:

// On Button Click
public void openIt(View view) {
    Intent intent = new Intent(view.getContext(), openPage.class);
    startActivity(intent); 
             }

在 MainActivity 中,而不是在“AFragment”中。

于 2013-04-02T00:04:45.800 回答