2017-02-26 179 views
-2

我想做一個應用程序有幾個功能的基礎上,我已經有一個網站的功能。基本上我正在創建一個伴侶應用程序。話雖如此,當我按下主頁上的按鈕啓動該功能時,該應用程序崩潰。我嘗試過每一種可以找到的解決方案,但都沒有成功。奇怪的部分是,只要沒有額外的代碼,除了在創建類時生成的代碼以外,它都可以工作。應用程序崩潰的按鈕點擊 - Android的工作室

這是我的主要活動:

package com.example.nicole.signal; 

import android.content.Intent; 
import android.os.Bundle; 

import android.support.v7.app.AppCompatActivity; 
import android.view.View; 
import android.view.Menu; 
import android.view.MenuItem; 
import android.widget.Button; 

import com.example.nicole.signal.R; 

public class MainActivity extends AppCompatActivity { 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 

     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 


// Button Calls 

     Button forumbtn = (Button) findViewById(R.id.forum_btn); 
     forumbtn.setOnClickListener(new View.OnClickListener() 
     { 
      @Override 
      public void onClick (View view) 
      { 
       startActivity(new Intent(MainActivity.this, ForumActivity.class)); 
      } 
     }); 

     Button webbtn = (Button) findViewById(R.id.Web_btn); 
     webbtn.setOnClickListener(new View.OnClickListener() 
     { 
      @Override 
      public void onClick (View view) 
      { 
       startActivity(new Intent(MainActivity.this, WebActivity.class)); 
      } 
     }); 

     Button chatbtn = (Button) findViewById(R.id.Chat_btn); 
     chatbtn.setOnClickListener(new View.OnClickListener() 
     { 
      @Override 
      public void onClick (View view) 
      { 
       startActivity(new Intent(MainActivity.this, ChatActivity.class)); 
      } 
     }); 

     Button videobtn = (Button) findViewById(R.id.Video_btn); 
     videobtn.setOnClickListener(new View.OnClickListener() 
     { 
      @Override 
      public void onClick (View view) 
      { 
       startActivity(new Intent(MainActivity.this, VideoActivity.class)); 
      } 
     }); 

    } 


    @Override 
    public boolean onCreateOptionsMenu(Menu menu) { 
     // Inflate the menu; this adds items to the action bar if it is present. 
     getMenuInflater().inflate(R.menu.menu_main, menu); 
     return true; 
    } 

    @Override 
    public boolean onOptionsItemSelected(MenuItem item) { 
     // Handle action bar item clicks here. The action bar will 
     // automatically handle clicks on the Home/Up button, so long 
     // as you specify a parent activity in AndroidManifest.xml. 
     int id = item.getItemId(); 

     //noinspection SimplifiableIfStatement 
     if (id == R.id.action_settings) { 
      return true; 
     } 

     return super.onOptionsItemSelected(item); 
    } 

}

和我打電話的是崩潰的一類:

package com.example.nicole.signal; 


import android.content.Intent; 
import android.net.Uri; 
import android.os.Bundle; 
import android.webkit.WebSettings; 
import android.webkit.WebView; 


public class WebActivity extends MainActivity 
{ 

    private final WebView mWebView = (WebView) findViewById(R.id.activity_web); 


    @Override 
    protected void onCreate(Bundle savedInstanceState) { 

     // Enable Javascript 
     WebSettings webSettings = mWebView.getSettings(); 
     webSettings.setBuiltInZoomControls(true); 
     webSettings.setJavaScriptEnabled(true); 
     Uri uri = Uri.parse("http://hnsignal.ca"); 
     Intent intent = new Intent(Intent.ACTION_VIEW, uri); 
     startActivity(intent); 

     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_web); 
    } 
} 

這裏是logcat的錯誤:

02-26 17:33:37.895 29086-29086/? E/Zygote: MountEmulatedStorage() 
02-26 17:33:37.895 29086-29086/? E/Zygote: v2 
02-26 17:33:37.915 29086-29086/? E/SELinux: [DEBUG] get_category: variable seinfo: default sensitivity: NULL, cateogry: NULL 
02-26 17:33:48.976 29086-29086/com.example.nicole.signal E/AndroidRuntime: FATAL EXCEPTION: main 
                      Process: com.example.nicole.signal, PID: 29086 
                      java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.nicole.signal/com.example.nicole.signal.WebActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.Window$Callback android.view.Window.getCallback()' on a null object reference 
                       at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2546) 
                       at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2758) 
                       at android.app.ActivityThread.access$900(ActivityThread.java:177) 
                       at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1448) 
                       at android.os.Handler.dispatchMessage(Handler.java:102) 
                       at android.os.Looper.loop(Looper.java:145) 
                       at android.app.ActivityThread.main(ActivityThread.java:5942) 
                       at java.lang.reflect.Method.invoke(Native Method) 
                       at java.lang.reflect.Method.invoke(Method.java:372) 
                       at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1399) 
                       at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1194) 
                      Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.Window$Callback android.view.Window.getCallback()' on a null object reference 
                       at android.support.v7.app.AppCompatDelegateImplBase.<init>(AppCompatDelegateImplBase.java:120) 
                       at android.support.v7.app.AppCompatDelegateImplV9.<init>(AppCompatDelegateImplV9.java:151) 
                       at android.support.v7.app.AppCompatDelegateImplV11.<init>(AppCompatDelegateImplV11.java:31) 
                       at android.support.v7.app.AppCompatDelegateImplV14.<init>(AppCompatDelegateImplV14.java:55) 
                       at android.support.v7.app.AppCompatDelegate.create(AppCompatDelegate.java:205) 
                       at android.support.v7.app.AppCompatDelegate.create(AppCompatDelegate.java:185) 
                       at android.support.v7.app.AppCompatActivity.getDelegate(AppCompatActivity.java:519) 
                       at android.support.v7.app.AppCompatActivity.findViewById(AppCompatActivity.java:190) 
                       at com.example.nicole.signal.WebActivity.<init>(WebActivity.java:14) 
                       at java.lang.reflect.Constructor.newInstance(Native Method) 
                       at java.lang.Class.newInstance(Class.java:1650) 
                       at android.app.Instrumentation.newActivity(Instrumentation.java:1079) 
                       at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2536) 
                       at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2758)  
                       at android.app.ActivityThread.access$900(ActivityThread.java:177)  
                       at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1448)  
                       at android.os.Handler.dispatchMessage(Handler.java:102)  
                       at android.os.Looper.loop(Looper.java:145)  
                       at android.app.ActivityThread.main(ActivityThread.java:5942)  
                       at java.lang.reflect.Method.invoke(Native Method)  
                       at java.lang.reflect.Method.invoke(Method.java:372)  
                       at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1399)  
                       at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1194)  

任何h艾爾普將不勝感激。在構建期間沒有錯誤拋出,所以我想它是在我的代碼中的某處...我只是無法找到它。謝謝

+0

可能重複[什麼是NullPointerException,以及如何解決它?](http://stackoverflow.com/questions/218384)/what-is-a-nullpointerexception-and-how-do-i-fix-it) –

+0

'at com.example.nicole.signal.WebActivity。 (WebActivity.java:14)'錯誤發生在WebActivity.java的第14行。這是哪一行? –

+0

當您調用'setContentView()'時,會創建視圖。在執行此操作之前,您無法訪問。 –

回答

1

你的代碼有幾個問題。首先

super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_web); 

這些必須是onCreate()的前兩行。 setContentView()創建視圖對象,因此直到調用此方法後才能訪問它們中的任何一個。這也意味着,你不能初始化字段變量在線如同你:

private final WebView mWebView = (WebView) findViewById(R.id.activity_web); 

相反,你必須這樣做的onCreate()內。您在MainActivity中完全正確地執行此操作。用它作爲指導。

你也應該改變WebActivity

public class WebActivity extends AppCompatActivity 

你不應該延長MainActivity

+0

我也認爲她想要在web視圖中加載該URI,但她實際上是否啓動了帶有此代碼的瀏覽器? –

+0

我照你說的做了,但仍得到相同的結果 –

+0

@IvoBeckers是的,似乎還有其他一些邏輯錯誤。目前,我只處理導致事故的NPE。 –

0
private final WebView mWebView = (WebView) findViewById(R.id.activity_web); 

這段代碼的問題,您應該聲明的WebView爲

private final WebView mWebView; 

像這樣

super.onCreate(savedInstanceState); 
setContentView(R.layout.activity_web); 
mWebView = (WebView) findViewById(R.id.activity_web); 
WebSettings webSettings = mWebView.getSettings(); 
webSettings.setBuiltInZoomControls(true); 
webSettings.setJavaScriptEnabled(true); 
Uri uri = Uri.parse("http://hnsignal.ca"); 
Intent intent = new Intent(Intent.ACTION_VIEW, uri); 
startActivity(intent); 

中的setContentView後,將其值設置,因爲的setContentView會膨脹的佈局,在佈局膨脹之前,您無法使用findViewById查找視圖。和所有你設置web視圖的代碼應該低於查找webview的代碼

相關問題