第一个应用程序,所以在android(大约一年前播放,但没有任何结果)真的很新,在编程方面体面。想要确保手机上的应用呈现,没有功能性atm。编译好,没有问题。把它推到我的手机上,“不幸的是,斯普利特已经停止了。”
编辑 - 添加到MainActivity.java,编辑有*之前和之后(不在实际代码中)但现在它标记
adapter.setDropDownViewResource(android.R.layout.activity_main);
// Apply the adapter to the spinner
spinnerSplit.setAdapter(adapter);
说错误:(18,36)标识符
setDropDownViewResource,activity_main和setAdapter全部为红色
Android Studio
Moto G - CM11 4.4.4
以下是代码,如果我遗漏了任何内容,请告诉我。
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)
感谢。
答案 0 :(得分:22)
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)
这告诉您错误正在衡量Spinner
。更准确地说,应该在其中显示的字符串之一是null。
最可能的原因是@array/arraySplitr
资源出现问题。它是有效的string-array
资源as defined in the documentation吗?
此:
<array name="arraySplitr">
<item>2</item>
...
应该是:
<string-array name="arraySplitr">
<item>2</item>
...
array
元素适用于Typed Arrays。字符串数组必须使用string-array
。
答案 1 :(得分:0)
您的logcat有一个NullPointerException。
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)
某些内容未初始化。从你的代码我看不出它会是什么。你有一个ArrayAdapter,如果你有,你初始化它和它包含的所有对象?或者您的表格是否由您的布局文件填充?我会开始研究你的表是如何填充的(基于logcat),但我不能具体看到你发布的代码会出现什么问题。