我正在尝试开发Android应用,但我无法让这个简单的单活动应用工作。我已经尝试了几天,我甚至改写了整个应用程序,我仍然不知道为什么它会给我这些错误。我尝试了不同的方式来接近搜索部分,但没有一个给我任何有用的答案。
现在我正在使用平板电脑加载应用。我知道它配置得很好,因为如果我加载一个hello world app它可以正常工作。我使用IntelliJ Idea,虽然我也尝试使用Eclipse,但结果相同。我在Ubuntu 14.04上使用IDE作为root用户(想法是root用户,Eclipse用作我的用户,因此我不认为这与它有任何关系,但值得一提)。我在eclipse上使用模拟器运行它,但它也没有工作。
该应用程序是一个简单的骰子生成器,有2个按钮,2个文本字段和填充屏幕其余部分的文本视图。任何形式的帮助将受到高度赞赏。我希望我没有超载我的帖子,对不起,如果是这样的话。
运行终端输出:
启动应用程序:com.bluehouse.Dices / com.bluehouse.Dices.DiceGen。 设备壳命令:我开始-n" com.bluehouse.Dices / com.bluehouse.Dices.DiceGen" -a android.intent.action.MAIN -c android.intent.category.LAUNCHER 开始:Intent {act = android.intent.action.MAIN cat = [android.intent.category.LAUNCHER] cmp = com.bluehouse.Dices / .DiceGen}
Logcat输出:
10-16 18:13:23.704 5005-5005/com.bluehouse.Dices E/AndroidRuntime﹕ FATAL EXCEPTION: main
java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.bluehouse.Dices/com.bluehouse.Dices.DiceGen}: java.lang.NullPointerException
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1983)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084)
at android.app.ActivityThread.access$600(ActivityThread.java:130)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4745)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NullPointerException
at android.content.ContextWrapper.getApplicationInfo(ContextWrapper.java:132)
at android.view.ContextThemeWrapper.getTheme(ContextThemeWrapper.java:65)
at android.app.AlertDialog.resolveDialogTheme(AlertDialog.java:142)
at android.app.AlertDialog$Builder.<init>(AlertDialog.java:359)
at com.bluehouse.Dices.DiceGen.<init>(DiceGen.java:186)
at java.lang.Class.newInstanceImpl(Native Method)
at java.lang.Class.newInstance(Class.java:1319)
at android.app.Instrumentation.newActivity(Instrumentation.java:1053)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1974)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084)
at android.app.ActivityThread.access$600(ActivityThread.java:130)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4745)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
at dalvik.system.NativeStart.main(Native Method)
10-16 18:13:26.294 5005-5005/com.bluehouse.Dices I/Process﹕ Sending signal. PID: 5005 SIG: 9
清单:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.bluehouse.Dices"
android:versionCode="1"
android:versionName="1.0">
<uses-sdk android:minSdkVersion="4"/>
<application android:label="@string/app_name" android:icon="@drawable/ic_launcher">
<activity android:name="DiceGen"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
</application>
代码:
public class DiceGen extends Activity {
EditText dicesEditText, sidesEditText;
Button saveButton, genButton;
TextView resTextView;
int nsides,ndices, tempns,tempnd;
int testigo1 = 0, testigo2 = 0;
/*parentlist is a list array of DiceList objects.
Each DiceList object holds an array of Dice(s) with the same number of sides.*/
List<DiceList> parentlist = null;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
dicesEditText = (EditText) findViewById(R.id.dicesEditText);
sidesEditText = (EditText) findViewById(R.id.sidesEditText);
saveButton = (Button) findViewById(R.id.saveButton);
genButton = (Button) findViewById(R.id.genButton);
resTextView = (TextView) findViewById(R.id.resTextView);
}
TextWatcher sidesTextWatcher = new TextWatcher() {
@Override
public void afterTextChanged(Editable editable) {
try {
tempns = Integer.parseInt(sidesEditText.getText().toString());
if (nsides <= 0) {
errorm.show();
} else {
nsides = tempns;
testigo1 = 1;
}
}
catch(NumberFormatException e){
errorm.show();
}
}
@Override
public void beforeTextChanged(CharSequence charSequence, int i, int i2, int i3) {
}
@Override
public void onTextChanged(CharSequence charSequence, int i, int i2, int i3) {
}
};
TextWatcher dicesTextWatcher = new TextWatcher() {
@Override
public void afterTextChanged(Editable editable) {
try {
tempnd = Integer.parseInt(dicesEditText.getText().toString());
if (ndices <= 0) {
errorm.show();
} else {
ndices = tempnd;
testigo2 = 1;
}
}
catch(NumberFormatException e){
errorm.show();
}
}
@Override
public void beforeTextChanged(CharSequence charSequence, int i, int i2, int i3) {
}
@Override
public void onTextChanged(CharSequence charSequence, int i, int i2, int i3) {
}
};
public void genDices(View saveButton) {
DiceList dicelist;
Dice dice;
if (testigo1 == 0 || testigo2 == 0) {
terror.show();
} else {
/*If the list is empty, there is no need to check if
there is already a DiceList holding that same kind of dice*/
if (parentlist.isEmpty()) {
dicelist = new DiceList(nsides);
dicelist.setAmount(ndices);
parentlist.add(dicelist);
for (int i = 0; i < ndices; i++) {
dice = new Dice(nsides);
dicelist.insert(dice.getResult());
}
} else {
/*If dices have been rolled, check for a DiceList with
the same kind of dice the input has*/
int parentindex = 0;
DiceList dicelistindex;
Boolean found = false;
for (int i = 0; i < parentlist.size(); i++) {
dicelistindex = parentlist.get(i);
parentindex = dicelistindex.getType();
/*If a match is found, insert dice result one by one there, if it's not,
create a new DiceList with the new value*/
if (parentindex == nsides) {
found = true;
for (i = 0; i < ndices; i++) {
dice = new Dice(nsides);
dicelistindex.insert(dice.getResult());
break;
}
}
}
if (!found) {
dicelist = new DiceList(nsides);
dicelist.setAmount(ndices);
parentlist.add(dicelist);
for (int i = 0; i < ndices; i++) {
dice = new Dice(nsides);
dicelist.insert(dice.getResult());
}
}
}
}
}
public void printDices(View genButton){
if (!parentlist.isEmpty()){
String todisplay="";
DiceList parentindex;
for (int i=0;i<parentlist.size();i++){
parentindex = parentlist.get(i);
todisplay = parentindex.getName()+parentindex.print();
}
resTextView.setText(todisplay);
parentlist.clear();
}
}
Builder errorm = new AlertDialog.Builder(this).setTitle("oops")
.setMessage(getString(R.string.errorm_body))
.setNegativeButton(android.R.string.ok, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
}});
Builder terror = new AlertDialog.Builder(this).setTitle("oops")
.setMessage(getString(R.string.terror_body))
.setNegativeButton(android.R.string.ok, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
}});
}
答案 0 :(得分:0)
尝试移动代码
Builder errorm = new AlertDialog.Builder(this).setTitle("oops")
.setMessage(getString(R.string.errorm_body))
.setNegativeButton(android.R.string.ok, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
}});
Builder terror = new AlertDialog.Builder(this).setTitle("oops")
.setMessage(getString(R.string.terror_body))
.setNegativeButton(android.R.string.ok, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
}});
在像这样的一些功能中
public void showErrorm()
{
Builder errorm = new AlertDialog.Builder(this).setTitle("oops")
.setMessage(getString(R.string.errorm_body))
.setNegativeButton(android.R.string.ok, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
}});
}
public void showErrort()
{
Builder terror = new AlertDialog.Builder(this).setTitle("oops")
.setMessage(getString(R.string.terror_body))
.setNegativeButton(android.R.string.ok, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
}});
}
稍后您可以在任何需要的地方调用此功能。
答案 1 :(得分:0)
<activity android:name=".DiceGen"
android:label="@string/app_name">
int testigo1 = 0, testigo2 = 0;
/*parentlist is a list array of DiceList objects.
Each DiceList object holds an array of Dice(s) with the same number of sides.*/
List<DiceList> parentlist = null;
int testigo1, testigo2;
/*parentlist is a list array of DiceList objects.
Each DiceList object holds an array of Dice(s) with the same number of sides.*/
List<DiceList> parentlist;
当我初始化数组和方法之外的很多变量时,我经常会遇到这个错误。
另外,没有必要用它的默认值来定义一个实例变量。
例如,Integers的默认值为0,而像ArrayList这样的引用类型的默认值为&#34; null&#34;。为什么我删除了初始化并且我没有删除定义任何变量