应用程序崩溃与intent方法

时间:2015-11-13 21:11:19

标签: java android android-intent

每当我尝试使用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); } } } 方法时,

我的应用都会崩溃。

MainActivity.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);
            }
        });
    }
}

AddIP.java

    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}}

2 个答案:

答案 0 :(得分:1)

更改

ArrayList<String> server_name_list = null;

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

因为server_name_list为null,所以当你到达server_name_list.add(new_server_name);时会抛出 NullPointerException ,并且执行失败。

答案 1 :(得分: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);

        }
    }
}