Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
590 views
in Technique[技术] by (71.8m points)

java - NullPointerException with string-array in Spinner

First app and so really new at android (played around about a year ago but nothing came of it), decent at programming. Wanting to make sure app renders on a phone, no functionality atm. Compiles fine, no problems there. Push it to my phone and "Unfortunately, Splitr has stopped".

Edit - added to the MainActivity.java, edits have * before and after (not in actual code) but now it flags

adapter.setDropDownViewResource(android.R.layout.activity_main);
// Apply the adapter to the spinner
spinnerSplit.setAdapter(adapter);

saying Error:(18, 36) identifier expected

setDropDownViewResource, activity_main and setAdapter all in red

Android Studio
Moto G - CM11 4.4.4

Here is the code, let me know if I missed anything out.

MainActivity.java

package com.hydr0dr4gon.splitrv2;

import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
*import android.widget.ArrayAdapter;
import android.widget.Spinner;*


public class MainActivity extends Activity {

    *Spinner spinnerSplit = (Spinner) findViewById(R.id.spinner);
// Create an ArrayAdapter using the string array and a default spinner layout
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this,
        R.array.arraySplitr, android.R.layout.simple_spinner_item);
// Specify the layout to use when the list of choices appears
adapter.setDropDownViewResource(android.R.layout.activity_main);
// Apply the adapter to the spinner
spinnerSplit.setAdapter(adapter);*

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


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

SplitrDialogFragment.java

package com.hydr0dr4gon.splitrv2;

import android.app.AlertDialog;
import android.app.Dialog;
import android.app.DialogFragment;
import android.content.DialogInterface;
import android.os.Bundle;

public class SplitrDialogFragment extends DialogFragment {

@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
    // Use the Builder class for convenient dialog construction
    AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());

    builder.setMessage(R.string.dialog_split_pay)
            .setNegativeButton(R.string.OK, new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int id) {
                    // User cancelled the dialog
                }
            });

    // Create the AlertDialog object and return it
    return builder.create();
 }
}

Strings.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <string name="app_name">Splitr</string>
    <string name="hello_world">Hello world!</string>
    <string name="action_settings">Settings</string>
    <string name="btnSplit">Split</string>
    <string name="btnTip">+ Tip</string>
    <string name="txtSplit">Split By</string>
    <string name="txtHint">Enter Bill Total</string>
    <string name="spinnerSplit">No. of People</string>
    <string name="dialog_split_pay">Each Person Pays</string>
    <string name="OK">OK</string>

<array name="arraySplitr">
    <item>2</item>
    <item>3</item>
    <item>4</item>
    <item>5</item>
    <item>6</item>
    <item>7</item>
    <item>8</item>
    <item>9</item>
    <item>10</item>
</array>

actvity_main.xml

RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
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.hydr0dr4gon.splitr.MainActivity">

<TableLayout
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:layout_alignParentTop="true"
    android:layout_alignParentStart="true"
    android:paddingLeft="40dp"
    android:paddingTop="80dp"
    android:paddingRight="40dp"
    android:paddingBottom="80dp">

    <TableRow
        android:layout_width="fill_parent"
        android:layout_height="fill_parent">

        <EditText
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_span="2"
            android:inputType="number"
            android:ems="10"
            android:id="@+id/editText"
            android:layout_column="0"
            android:layout_weight="1.5"
            android:hint="@string/txtHint" />

    </TableRow>

    <TableRow
        android:layout_width="fill_parent"
        android:layout_height="fill_parent">

        <TextView
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:text="@string/txtSplit"
            android:id="@+id/txtSplit"
            android:layout_weight="1"
            android:textSize="32dp"
            android:textAlignment="center"
            android:gravity="center" />

        <Spinner
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:id="@+id/spinner"
            android:layout_weight="1"
            android:spinnerStyle="@android:style/Widget.Spinner.DropDown"
            android:textAlignment="center"
            android:entries="@array/arraySplitr"
            android:prompt="@string/spinnerSplit"/>

    </TableRow>

    <TableRow
        android:layout_width="fill_parent"
        android:layout_height="fill_parent">

        <Button
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:text="@string/btnTip"
            android:id="@+id/btnTip"
            android:layout_weight="1" />

        <Button
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:text="@string/btnSplit"
            android:id="@+id/btnSplit"
            android:layout_weight="1" />
    </TableRow>

</TableLayout>

logcat

> 07-11 21:18:50.899  10290-10290/com.hydr0dr4gon.splitrv2 D/ActivityThread﹕ handleBindApplication:com.hydr0dr4gon.splitrv2
07-11 21:18:50.899  10290-10290/com.hydr0dr4gon.splitrv2 D/ActivityThread﹕ setTargetHeapUtilization:0.75
07-11 21:18:50.899  10290-10290/com.hydr0dr4gon.splitrv2 D/ActivityThread﹕ setTargetHeapMinFree:2097152
07-11 21:18:51.198  10290-10290/com.hydr0dr4gon.splitrv2 D/AndroidRuntime﹕ Shutting down VM
07-11 21:18:51.206  10290-10290/com.hydr0dr4gon.splitrv2 E/AndroidRuntime﹕ FATAL EXCEPTION: main
    Process: com.hydr0dr4gon.splitrv2, PID: 10290
    java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String java.lang.Object.toString()' on a null object reference
            at android.widget.ArrayAdapter.createViewFromResource(ArrayAdapter.java:394)
            at android.widget.ArrayAdapter.getView(ArrayAdapter.java:362)
            at android.widget.AbsSpinner.onMeasure(AbsSpinner.java:193)
            at android.widget.Spinner.onMeasure(Spinner.java:482)
            at android.view.View.measure(View.java:16521)
            at android.widget.TableRow.getColumnsWidths(TableRow.java:312)
            at android.widget.TableLayout.findLargestCells(TableLayout.java:508)
            at android.widget.TableLayout.measureVertical(TableLayout.java:473)
            at android.widget.TableLayout.onMeasure(TableLayout.java:439)
            at android.view.View.measure(View.java:16521)
            at android.widget.RelativeLayout.measureChildHorizontal(RelativeLayout.java:719)
            at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:455)
            at android.view.View.measure(View.java:16521)
            at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5125)
            at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
            at android.view.View.measure(View.java:16521)
            at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5125)
            at com.android.internal.widget.ActionBarOverlayLayout.onMeasure(ActionBarOverlayLayout.java:327)
            at android.view.View.measure(View.java:16521)
            at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5125)
            at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
            at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2552)
            at android.view.View.measure(View.java:16521)
            at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:1912)
            at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1109)
            at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1291)
            at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:996)
            at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:5600)
            at android.view.Choreographer$CallbackRecord.run(Choreographer.java:761)
            at android.view.Choreographer.doCallbacks(Choreographer.java:574)
            at android.view.Choreographer.doFrame(Choreographer.java:544)
            at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:747)
            at android.os.Handler.handleCallback(Handler.java:733)
            at android.os.Handler.dispatchMessage(Handler.java:95)
            at android.os.Looper.loop(Looper.java:136)
            at android.app.ActivityThread.main(ActivityThread.java:5137)
            at java.lang.reflect.Method.invoke(Native Method)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:795)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:611)

Thanks.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String java.lang.Object.toString()' on a null object reference
    at android.widget.ArrayAdapter.createViewFromResource(ArrayAdapter.java:394)
    at android.widget.ArrayAdapter.getView(ArrayAdapter.java:362)
    at android.widget.AbsSpinner.onMeasure(AbsSpinner.java:193)
    at android.widget.Spinner.onMeasure(Spinner.java:482)

This tells you that the error was measuring the Spinner. More exactly, one of the strings that should be displayed inside it is null.

The most likely cause is a problem with the @array/arraySplitr resource. Is it a valid string-array resource as defined in the documentation?

This:

<array name="arraySplitr">
    <item>2</item>
    ...

should be instead:

<string-array name="arraySplitr">
    <item>2</item>
    ...

The array element is for Typed Arrays. String arrays must use string-array.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...