1
我試圖運行示例代碼當在Android 5.1模擬器中啓動應用程序時,我得到了NULL POINTER EXCEPTION的錯誤....任何人都有一些提示..我的活動文件註冊在Android清單文件,但我的課的2壓根沒有註冊無法實例化活動:Android的webview中的Nullpointerexception
MainActivity.class
public class MainActivity extends Activity implements TabListener {
WebView webView;
Button button;
MainFragment mainFragment;
ImageButton imageButton;
TextView txtview;
EditText editText;
JavaScriptInterface jSInterface;
ActionBar bar = getActionBar();
@SuppressWarnings("deprecation")
@SuppressLint({ "SetJavaScriptEnabled", "NewApi" })
@Override
protected void onCreate(Bundle savedInstanceState) {
Log.d("Main Activity", "Inside on create");
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
setStatusBarColor(this, Color.parseColor("#4CAF50"));
webView = (WebView) findViewById(R.id.webPage);
webView.getSettings().setJavaScriptEnabled(true);
jSInterface = new JavaScriptInterface(this);
webView.addJavascriptInterface(jSInterface, "JSInterface");
webView.getSettings().setLoadsImagesAutomatically(true);
webView.setWebChromeClient(new WebChromeClient());
System.out.println("Opening webview");
webView.loadUrl("file:///android_asset/index2.html");
Tab tab1 = bar.newTab();
tab1.setText("tab1");
tab1.setTabListener(this);
bar.addTab(tab1);
Tab tab2 = bar.newTab();
tab2.setText("tab2");
tab2.setTabListener(this);
bar.addTab(tab2);
Tab tab3 = bar.newTab();
tab3.setText("tab3");
tab3.setTabListener(this);
bar.addTab(tab3);
imageButton=(ImageButton)findViewById(R.id.imagebutton1);
editText=(EditText)findViewById(R.id.editText1);
Fragment main = new MainFragment();
mainFragment=(MainFragment)main;
FragmentTransaction ft = getFragmentManager().beginTransaction();
ft.replace(R.id.content_frame, main);
ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
ft.addToBackStack(null);
ft.commit();
}
顯示Logcat並使用AppCompatActivity擴展您的類。 – Shvet
我的logcat是一樣的http://stackoverflow.com/questions/4688277/java-lang-runtimeexception-unable-to-instantiate-activity-componentinfo但這裏提到的解決方案不適合我 – Loren
你可以請調試你的代碼並告訴我們你在哪一行得到'NullPointerException'或者請在這裏發佈logcat日誌,以便我們可以獲得該行。 –