如果我尝试打开我的片段,应用程序崩溃

时间:2015-11-14 06:57:30

标签: java android android-fragments

如果我尝试打开我的AddIP.class fragment

,我的应用程序崩溃了

我的错误代码显示:

  

java.lang.RuntimeException:无法实例化活动ComponentInfo {de.kwietzorek.fulcrumwebview / de.kwietzorek.fulcrumwebview.MainActivity $ AddIP}:java.lang.InstantiationException:java.lang.Class没有零参数构造函数

的AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="de.kwietzorek.fulcrumwebview" >

<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:supportsRtl="true">
    <activity android:name=".MainActivity" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <activity android:name=".MainActivity$AddIP"></activity>
</application>

<uses-permission android:name="android.permission.INTERNET" />

</manifest>

MainActivity.java

package de.kwietzorek.fulcrumwebview;


import android.content.Intent;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
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.Button;
import android.widget.EditText;
import android.widget.Spinner;

import java.util.ArrayList;


public class MainActivity extends FragmentActivity {

    String selected;
    Spinner spinner;
    WebView myWebView;
    ArrayList<String> server_name_list = null;

    /* 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

        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);
            server_adapter.notifyDataSetChanged();

            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) {

                }
            });
        }
    }

    //WebView Client
    public class WebC extends WebViewClient {

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

            super.onReceivedError(view, errorCode, description, failingUrl);

        }
    }

    public class AddIP extends Fragment {

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

        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.add_ip);

            server_ip = (EditText) findViewById(R.id.edit_server_address);
            server_name = (EditText) 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);*/

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

        }
    }
}

1 个答案:

答案 0 :(得分:0)

将此方法添加到您的文章:

public AddIP(){ }

在某些情况下,您的片段需要一个空的构造函数方法