2015-11-13 176 views
2

每當我嘗試使用intent方法時,我的應用程序崩潰。應用程序意圖崩潰方法

MainActivity.java

package de.kwietzorek.fulcrumwebview; 


import android.content.Intent; 
import android.support.v7.app.AppCompatActivity; 
import android.os.Bundle; 
import android.view.Menu; 
import android.view.MenuInflater; 
import android.view.MenuItem; 
import android.view.View; 
import android.webkit.WebSettings; 
import android.webkit.WebView; 
import android.webkit.WebViewClient; 
import android.widget.AdapterView; 
import android.widget.ArrayAdapter; 
import android.widget.Spinner; 

import java.util.ArrayList; 

public class MainActivity extends AppCompatActivity { 

    String selected; 
    Spinner spinner; 
    WebView myWebView; 

    /* Menu */ 
    @Override 
    public boolean onCreateOptionsMenu(Menu menu) { 
     MenuInflater inflater = getMenuInflater(); 
     inflater.inflate(R.menu.main, menu); 
     return true; 
    } 

    @Override 
    public boolean onOptionsItemSelected(MenuItem item) { 
     // Handle item selection 
     switch (item.getItemId()) { 
      case R.id.add_server: 
       Intent intent = new Intent(this, AddIP.class); 
       this.startActivity(intent); 
       return true; 
      case R.id.menu_refresh: 
       myWebView.reload(); 
       return true; 
      default: 
       return true; 
     } 
    } 


    @Override 
    protected void onCreate(Bundle savedInstanceState) { 

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

     //WebView 
     myWebView = (WebView) findViewById(R.id.webView); 
     myWebView.setWebViewClient(new WebC()); 
     WebSettings webSettings = myWebView.getSettings(); 

     //JavaScript enable 
     webSettings.setJavaScriptEnabled(true); 

     //Server name spinner 
     ArrayList<String> server_name_list = getIntent().getStringArrayListExtra(
       "int_new_server_name"); 

     spinner = (Spinner) findViewById(R.id.server_spinner); 

     ArrayAdapter<String> server_adapter = new ArrayAdapter<String>(this, 
       android.R.layout.simple_spinner_item, server_name_list); 

     server_adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); 
     spinner.setAdapter(server_adapter); 

     spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() { 

      public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) { 

       selected = parent.getItemAtPosition(pos).toString(); 

       myWebView.loadUrl(selected); 
      } 

      public void onNothingSelected(AdapterView<?> parent) { 

      } 
     }); 
    } 

    //WebVie Client 
     public class WebC extends WebViewClient { 

     @Override 
     public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) { 
      super.onReceivedError(view, errorCode, description, failingUrl); 

     } 
    } 
} 

AddIP.java

package de.kwietzorek.fulcrumwebview; 

import android.content.Intent; 
import android.os.Bundle; 
import android.support.v7.app.AppCompatActivity; 
import android.view.View; 
import android.widget.Button; 
import android.widget.EditText; 

import java.util.ArrayList; 


public class AddIP extends AppCompatActivity { 

    Button btn_back, btn_add; 
    EditText server_ip, server_name; 
    String new_server_ip, new_server_name; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 

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

     server_ip = (EditText)this.findViewById(R.id.edit_server_address); 
     server_name = (EditText)this.findViewById(R.id.edit_server_name); 

     /* Back Button */ 
     btn_back = (Button)findViewById(R.id.btn_back); 
     btn_back.setOnClickListener(new View.OnClickListener() { 

      @Override 
      public void onClick(View view) { 
       Intent intent = new Intent(getApplicationContext(), MainActivity.class); 
       startActivity(intent); 
      } 
     }); 

     /* Add IP Button */ 
     btn_add = (Button)findViewById(R.id.btn_add); 
     btn_add.setOnClickListener(new View.OnClickListener() { 

      @Override 
      public void onClick(View view) { 
       /*new_server_ip = server_ip.getText().toString(); 
       MainActivity.server_array_ip.add(new_server_ip);*/ 

       ArrayList<String> server_name_list = new ArrayList<String>(); 

       new_server_name = server_name.getText().toString(); 
       server_name_list.add(new_server_name); 

       Intent intent = new Intent(AddIP.this, MainActivity.class); 
       intent.putStringArrayListExtra("int_server_name_list", server_name_list); 
      } 
     }); 
    } 
} 

錯誤

11-13 22:13:49.542 4035-4035/de.kwietzorek.fulcrumwebview E/AndroidRuntime: FATAL EXCEPTION: main 
11-13 22:13:49.542 4035-4035/de.kwietzorek.fulcrumwebview E/AndroidRuntime: Process: de.kwietzorek.fulcrumwebview, PID: 4035 
11-13 22:13:49.542 4035-4035/de.kwietzorek.fulcrumwebview E/AndroidRuntime: java.lang.RuntimeException: Unable to start activity ComponentInfo{de.kwietzorek.fulcrumwebview/de.kwietzorek.fulcrumwebview.MainActivity}: java.lang.NullPointerException: Attempt to invoke interface method 'int java.util.List.size()' on a null object reference 
11-13 22:13:49.542 4035-4035/de.kwietzorek.fulcrumwebview E/AndroidRuntime:  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2416) 
11-13 22:13:49.542 4035-4035/de.kwietzorek.fulcrumwebview E/AndroidRuntime:  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476) 
11-13 22:13:49.542 4035-4035/de.kwietzorek.fulcrumwebview E/AndroidRuntime:  at android.app.ActivityThread.-wrap11(ActivityThread.java) 
11-13 22:13:49.542 4035-4035/de.kwietzorek.fulcrumwebview E/AndroidRuntime:  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344) 
11-13 22:13:49.542 4035-4035/de.kwietzorek.fulcrumwebview E/AndroidRuntime:  at android.os.Handler.dispatchMessage(Handler.java:102) 
11-13 22:13:49.542 4035-4035/de.kwietzorek.fulcrumwebview E/AndroidRuntime:  at android.os.Looper.loop(Looper.java:148) 
11-13 22:13:49.542 4035-4035/de.kwietzorek.fulcrumwebview E/AndroidRuntime:  at android.app.ActivityThread.main(ActivityThread.java:5417) 
11-13 22:13:49.542 4035-4035/de.kwietzorek.fulcrumwebview E/AndroidRuntime:  at java.lang.reflect.Method.invoke(Native Method) 
11-13 22:13:49.542 4035-4035/de.kwietzorek.fulcrumwebview E/AndroidRuntime:  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) 
11-13 22:13:49.542 4035-4035/de.kwietzorek.fulcrumwebview E/AndroidRuntime:  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) 
11-13 22:13:49.542 4035-4035/de.kwietzorek.fulcrumwebview E/AndroidRuntime: Caused by: java.lang.NullPointerException: Attempt to invoke interface method 'int java.util.List.size()' on a null object reference 
11-13 22:13:49.542 4035-4035/de.kwietzorek.fulcrumwebview E/AndroidRuntime:  at android.widget.ArrayAdapter.getCount(ArrayAdapter.java:337) 
11-13 22:13:49.542 4035-4035/de.kwietzorek.fulcrumwebview E/AndroidRuntime:  at android.widget.AbsSpinner.setAdapter(AbsSpinner.java:115) 
11-13 22:13:49.542 4035-4035/de.kwietzorek.fulcrumwebview E/AndroidRuntime:  at android.widget.Spinner.setAdapter(Spinner.java:508) 
11-13 22:13:49.542 4035-4035/de.kwietzorek.fulcrumwebview E/AndroidRuntime:  at android.support.v7.widget.AppCompatSpinner.setAdapter(AppCompatSpinner.java:383) 
11-13 22:13:49.542 4035-4035/de.kwietzorek.fulcrumwebview E/AndroidRuntime:  at de.kwietzorek.fulcrumwebview.MainActivity.onCreate(MainActivity.java:75) 
11-13 22:13:49.542 4035-4035/de.kwietzorek.fulcrumwebview E/AndroidRuntime:  at android.app.Activity.performCreate(Activity.java:6237) 
11-13 22:13:49.542 4035-4035/de.kwietzorek.fulcrumwebview E/AndroidRuntime:  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1107) 
11-13 22:13:49.542 4035-4035/de.kwietzorek.fulcrumwebview E/AndroidRuntime:  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2369) 
11-13 22:13:49.542 4035-4035/de.kwietzorek.fulcrumwebview E/AndroidRuntime:  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)  
11-13 22:13:49.542 4035-4035/de.kwietzorek.fulcrumwebview E/AndroidRuntime:  at android.app.ActivityThread.-wrap11(ActivityThread.java)  
11-13 22:13:49.542 4035-4035/de.kwietzorek.fulcrumwebview E/AndroidRuntime:  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)  
11-13 22:13:49.542 4035-4035/de.kwietzorek.fulcrumwebview E/AndroidRuntime:  at android.os.Handler.dispatchMessage(Handler.java:102)  
11-13 22:13:49.542 4035-4035/de.kwietzorek.fulcrumwebview E/AndroidRuntime:  at android.os.Looper.loop(Looper.java:148)  
11-13 22:13:49.542 4035-4035/de.kwietzorek.fulcrumwebview E/AndroidRuntime:  at android.app.ActivityThread.main(ActivityThread.java:5417)  
11-13 22:13:49.542 4035-4035/de.kwietzorek.fulcrumwebview E/AndroidRuntime:  at java.lang.reflect.Method.invoke(Native Method)  
11-13 22:13:49.542 4035-4035/de.kwietzorek.fulcrumwebview E/AndroidRuntime:  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)  
11-13 22:13:49.542 4035-4035/de.kwietzorek.fulcrumwebview E/AndroidRuntime:  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)  

回答

1

查找範圍MainActivity

//Server name spinner 
    ArrayList<String> server_name_list = getIntent().getStringArrayListExtra(
      "int_new_server_name"); 

而且在AddIP

intent.putStringArrayListExtra("int_server_name_list", server_name_list); 

你的鍵(名字)是不同的,所以你想獲得的東西,不存在的,這就是爲什麼NullPointerException被拋出。


代碼爲您MainActivity

package de.kwietzorek.fulcrumwebview; 


import android.content.Intent; 
import android.support.v7.app.AppCompatActivity; 
import android.os.Bundle; 
import android.view.Menu; 
import android.view.MenuInflater; 
import android.view.MenuItem; 
import android.view.View; 
import android.webkit.WebSettings; 
import android.webkit.WebView; 
import android.webkit.WebViewClient; 
import android.widget.AdapterView; 
import android.widget.ArrayAdapter; 
import android.widget.Spinner; 

import java.util.ArrayList; 

public class MainActivity extends AppCompatActivity { 

    String selected; 
    Spinner spinner; 
    WebView myWebView; 

    /* Menu */ 
    @Override 
    public boolean onCreateOptionsMenu(Menu menu) { 
     MenuInflater inflater = getMenuInflater(); 
     inflater.inflate(R.menu.main, menu); 
     return true; 
    } 

    @Override 
    public boolean onOptionsItemSelected(MenuItem item) { 
     // Handle item selection 
     switch (item.getItemId()) { 
      case R.id.add_server: 
       Intent intent = new Intent(this, AddIP.class); 
       this.startActivity(intent); 
       return true; 
      case R.id.menu_refresh: 
       myWebView.reload(); 
       return true; 
      default: 
       return true; 
     } 
    } 


    @Override 
    protected void onCreate(Bundle savedInstanceState) { 

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

     //WebView 
     myWebView = (WebView) findViewById(R.id.webView); 
     myWebView.setWebViewClient(new WebC()); 
     WebSettings webSettings = myWebView.getSettings(); 

     //JavaScript enable 
     webSettings.setJavaScriptEnabled(true); 

     //Server name spinner 
     ArrayList<String> server_name_list = getIntent().getStringArrayListExtra(
       "int_new_server_name"); 

     if (server_name_list != null) { 

     spinner = (Spinner) findViewById(R.id.server_spinner); 

     ArrayAdapter<String> server_adapter = new ArrayAdapter<String>(this, 
       android.R.layout.simple_spinner_item, server_name_list); 

     server_adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); 
     spinner.setAdapter(server_adapter); 

     spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() { 

      public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) { 

       selected = parent.getItemAtPosition(pos).toString(); 

       myWebView.loadUrl(selected); 
      } 

      public void onNothingSelected(AdapterView<?> parent) { 

      } 
     }); 
     } 
    } 

    //WebVie Client 
     public class WebC extends WebViewClient { 

     @Override 
     public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) { 
      super.onReceivedError(view, errorCode, description, failingUrl); 

     } 
    } 
} 
+0

哦謝謝,改變它對同一個關鍵。但仍然得到錯誤 – Sorrow91

+0

嘗試自己調試它 - 在'spinner =(Spinner)findViewById(R.id.server_spinner);'中創建斷點並檢查'server_name_list'中的內容。 –

+1

等等,等等。你開始的第一項活動是什麼?如果它是'MainActivity',不要指望'getIntent()。getStringArrayListExtra( 「int_new_server_name」);'因爲你甚至沒有啓動AddIP'活動。您必須在「MainActivity」中禁用微調器。檢查我的編輯。 –

1

變化

ArrayList<String> server_name_list = null; 

ArrayList<String> server_name_list = new ArrayList<String>(); 

因爲server_name_list爲空,當你到一個server_name_list.add(new_server_name); NullPointerException異常拋出,並執行失敗。

+0

感謝 改變,但它仍然崩潰 – Sorrow91

+0

什麼是你的錯誤? –

+0

編輯我的問題 – Sorrow91