原生后退按钮工作,触后按钮崩溃?

时间:2014-08-18 21:17:50

标签: android android-activity

我可以使用本机后退按钮在我的应用程序中前进和后退。如果我使用触摸按钮,应用程序将通过附加的Stacktrace停止。

只在一个视图中,它包含一个WebView。我不想像所有其他人一样为WebView实现一个后退按钮,他们在这里提出了类似的问题(回页而不是活动回来)。

我还附上了应用程序的一些代码。它会在活动加载额外内容时崩溃(NullPointerException)。我可以避免,后退按钮会创建一个全新的活动吗?在我的应用程序的其他部分,这工作没有问题,即使有额外的移交

我尝试了什么: 使用Bundle并将附加内容直接放入intent中。

堆栈跟踪:

08-18 22:52:04.308    6982-6982/com.nubage.minepedia E/AndroidRuntime﹕ FATAL EXCEPTION: main
    Process: com.nubage.minepedia, PID: 6982
    java.lang.RuntimeException: Unable to start activity ComponentInfo{com.nubage.minepedia/com.nubage.minepedia.ItemsView}: java.lang.NullPointerException
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2215)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2265)
            at android.app.ActivityThread.access$800(ActivityThread.java:145)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1206)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:136)
            at android.app.ActivityThread.main(ActivityThread.java:5081)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:515)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:791)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:607)
            at dalvik.system.NativeStart.main(Native Method)
     Caused by: java.lang.NullPointerException
            at com.nubage.minepedia.ItemsView.onCreate(ItemsView.java:36)
            at android.app.Activity.performCreate(Activity.java:5231)
            at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2169)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2265)
            at android.app.ActivityThread.access$800(ActivityThread.java:145)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1206)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:136)
            at android.app.ActivityThread.main(ActivityThread.java:5081)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:515)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:791)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:607)
            at dalvik.system.NativeStart.main(Native Method)

ItemsMenu:

public class ItemsMenu extends Activity {

    private final String MENU_NAME = "items";
    private final String KEY_CATEGORY = "category";
    private final String KEY_SUBCATEGORY = "subcategory";

// .....

    public void itemButtonClicked(View view) {
        String parameter = null;
        Log.d("ID: ", String.valueOf(view.getId()));
        switch (view.getId()) {
            case R.id.building_blocks_id:
                parameter = getString(R.string.building_blocks);
                break;
            case R.id.decoration_blocks_id:
                parameter = getString(R.string.decoration_blocks);
                break;
            case R.id.redstone_id:
                parameter = getString(R.string.redstone);
                break;
            case R.id.transportation_id:
                parameter = getString(R.string.transportation);
                break;
            case R.id.miscellaneous_id:
                parameter = getString(R.string.miscellaneous);
                break;
            case R.id.foodstuffs_id:
                parameter = getString(R.string.foodstuffs);
                break;
            case R.id.tools_id:
                parameter = getString(R.string.tools);
                break;
            case R.id.combat_id:
                parameter = getString(R.string.combat);
                break;
            case R.id.brewing_id:
                parameter = getString(R.string.brewing);
                break;
            case R.id.materials_id:
                parameter = getString(R.string.materials);
                break;
        }
        Log.d("ItemsMenu - itemButtonClicked() parameter: ", parameter);
        if (parameter != null) {
            Intent itemsView = new Intent(this, ItemsView.class);
            Bundle bundle = new Bundle();
            bundle.putString(KEY_CATEGORY, MENU_NAME);
            bundle.putString(KEY_SUBCATEGORY, parameter);
            itemsView.putExtras(bundle);
            startActivity(itemsView);
        }
    }
}

ItemsView.class:

public class ItemsView extends Activity {

    private final String MENU_NAME = "items";
    private final String KEY_CATEGORY = "category";
    private final String KEY_SUBCATEGORY = "subcategory";


    GridView gridView;
    GridViewCustomAdapter gridViewCustomAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_items_view);

        String category = this.getIntent().getExtras().getString(KEY_CATEGORY);
        String subcategory = this.getIntent().getExtras().getString(KEY_SUBCATEGORY);

        /*if (this.getIntent().getExtras().getString(KEY_CATEGORY) != null)
            this.category = this.getIntent().getExtras().getString(KEY_CATEGORY);
        if (this.getIntent().getExtras().getString(KEY_SUBCATEGORY) != null)
            this.subcategory = this.getIntent().getExtras().getString(KEY_SUBCATEGORY);
*/
        Log.d("ItemsView - onCreate() - Bundle Category: " + category, "Length: " + category.length());
        Log.d("ItemsView - onCreate() - Bundle Category: " + subcategory, "Length: " + subcategory.length());

//        String[] databaseArray = new String[Minepedia_main.database.size()];

//        for (int i=0; i<Minepedia_main.database.size(); ++i)
//            databaseArray[i] = Minepedia_main.database.get(i);

        //Search the Database for wanted entries...
        List<String> resultCollection = new ArrayList<String>(150);
        for (int i=0; i<Minepedia_main.database.size(); ++i) {
            String temp = Minepedia_main.database.get(i);
            String[] split = temp.split(Pattern.quote("|"));

            if (split[2].equals(category) && split[3].equals(subcategory)) {
                resultCollection.add(temp);
            }

            /*if (temp.contains(category) && temp.contains(subcategory)) {
                resultCollection.add(temp);
            }*/
        }

        //List to Array...
        final String[] databaseArray = new String[resultCollection.size()];
        for (int i=0; i<resultCollection.size(); ++i) {
            databaseArray[i] = resultCollection.get(i);
        }

        gridView = (GridView)findViewById(R.id.gridView1);
        // Set the Adapter to GridView
        gridView.setAdapter(new GridViewCustomAdapter(this, databaseArray));

        // Handling touch/click Event on GridView Item
        gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> arg0, View v, int position, long arg3) {

                if (Minepedia_main.ONLINE_MODE_ENABLED == false) {
                    String fileName = databaseArray[position].split(Pattern.quote("|"))[0] + ".html";
                    createOfflineIntent(fileName);
                } else {
                    String url = Minepedia_main.PROVIDER + databaseArray[position].split(Pattern.quote("|"))[4];
                    createOnlineIntent(url);
                }
            }
        });
    }

    private void createOfflineIntent(String fileName) {
        Intent offlineBrowser = new Intent(this, ItemViewDetailOffline.class);
        //offlineBrowser.putExtra("fileName", fileName);
        Bundle bundle = new Bundle();
        bundle.putString("fileName", fileName);
        offlineBrowser.putExtras(bundle);
        startActivity(offlineBrowser);
    }

    private void createOnlineIntent(String url) {
        Intent onlineBrowser = new Intent(this, ItemViewDetailOnline.class);
        //onlineBrowser.putExtra("url", url);
        Bundle bundle = new Bundle();
        bundle.putString("url", url);
        onlineBrowser.putExtras(bundle);
        startActivity(onlineBrowser);
    }
}

ItemViewDetailOnline.class:

public class ItemViewDetailOnline extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_item_view_detail_online);

        String url = this.getIntent().getExtras().getString("url");

        WebView webview = (WebView) findViewById(R.id.webview_online);
        webview.loadUrl(url);
    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.item_view_detail, 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();
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }
}

activity_item_view_detail_online.xml:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:paddingBottom="@dimen/activity_vertical_margin"
    tools:context="com.nubage.minepedia.ItemViewDetailOnline">

    <TextView
        android:text="@string/hello_world"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
    <WebView
        android:id="@+id/webview_online"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"/>
</LinearLayout>

0 个答案:

没有答案