- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我是 Android 编程新手,只有一点 Java 背景。由于某种原因,我根据新波士顿 Android 教程系列制作的程序中的 3 个类创建了与此类似的错误:
03-13 18:17:22.095: E/AndroidRuntime(13055): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.infitenothing.dogcat/com.infitenothing.word.OpenClass}: java.lang.NullPointerException
03-13 18:17:22.095: E/AndroidRuntime(13055): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195)
03-13 18:17:22.095: E/AndroidRuntime(13055): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
03-13 18:17:22.095: E/AndroidRuntime(13055): at android.app.ActivityThread.access$800(ActivityThread.java:135)
03-13 18:17:22.095: E/AndroidRuntime(13055): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
03-13 18:17:22.095: E/AndroidRuntime(13055): at android.os.Handler.dispatchMessage(Handler.java:102)
03-13 18:17:22.095: E/AndroidRuntime(13055): at android.os.Looper.loop(Looper.java:136)
03-13 18:17:22.095: E/AndroidRuntime(13055): at android.app.ActivityThread.main(ActivityThread.java:5017)
03-13 18:17:22.095: E/AndroidRuntime(13055): at java.lang.reflect.Method.invokeNative(Native Method)
03-13 18:17:22.095: E/AndroidRuntime(13055): at java.lang.reflect.Method.invoke(Method.java:515)
03-13 18:17:22.095: E/AndroidRuntime(13055): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
03-13 18:17:22.095: E/AndroidRuntime(13055): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
03-13 18:17:22.095: E/AndroidRuntime(13055): at de.robv.android.xposed.XposedBridge.main(XposedBridge.java:126)
03-13 18:17:22.095: E/AndroidRuntime(13055): at dalvik.system.NativeStart.main(Native Method)
03-13 18:17:22.095: E/AndroidRuntime(13055): Caused by: java.lang.NullPointerException
03-13 18:17:22.095: E/AndroidRuntime(13055): at com.infitenothing.word.OpenClass.intalize(OpenClass.java:43)
03-13 18:17:22.095: E/AndroidRuntime(13055): at com.infitenothing.word.OpenClass.onCreate(OpenClass.java:29)
03-13 18:17:22.095: E/AndroidRuntime(13055): at android.app.Activity.performCreate(Activity.java:5231)
03-13 18:17:22.095: E/AndroidRuntime(13055): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
03-13 18:17:22.095: E/AndroidRuntime(13055): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)
03-13 18:17:22.095: E/AndroidRuntime(13055): ... 12 more
我用这个类启动了 Activity
public class Menu extends ListActivity {
String classes[] = {"MainActivity", "Splash", "TextPlay", "Email", "Camera", "DatA", "OpenClass", "example1", "example1"};
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setListAdapter(new ArrayAdapter<String>(Menu.this, android.R.layout.simple_list_item_1, classes ));
}
@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
// TODO Auto-generated method stub
super.onListItemClick(l, v, position, id);
String cheese = classes[position];
try{
Class ourClass = Class.forName("com.infitenothing.word." + cheese);
Intent ourIntent = new Intent(Menu.this, ourClass);
startActivity(ourIntent);
}catch(ClassNotFoundException e){
e.printStackTrace();
}
}
}
这些是不起作用的类
package com.infitenothing.word;
import com.infitenothing.dogcat.R;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class Data extends Activity implements OnClickListener {
Button start, startFor;
TextView gotAnswer;
EditText sendET;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
intalize();
setContentView(R.layout.get);
}
private void intalize() {
start = (Button) findViewById(R.id.bSA);
startFor = (Button) findViewById(R.id.bSAFR);
gotAnswer = (TextView) findViewById(R.id.tvGot);
sendET= (EditText) findViewById(R.id.etSend);
start.setOnClickListener(this);
startFor.setOnClickListener(this);
}
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
switch (arg0.getId()){
case R.id.bSA:
String bread = sendET.getText().toString();
Bundle basket = new Bundle();
basket.putString("key", bread);
Intent a = new Intent(Data.this, OpenClass.class);
a.putExtras(basket);
startActivity(a);
break;
case R.id.bSAFR:
break;
}
}
}
和
package com.infitenothing.word;
import java.util.Random;
import com.infitenothing.dogcat.R;
import android.R.anim;
import android.R.integer;
import android.R.string;
import android.app.Activity;
import android.graphics.Color;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.support.v4.widget.SimpleCursorAdapter.ViewBinder;
import android.text.InputType;
import android.view.Gravity;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.ToggleButton;
public class TextPlay extends Activity implements View.OnClickListener {
Button chkCmd;
ToggleButton passTog;
EditText input;
TextView display;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.text);
baconAndEggs();
passTog.setOnClickListener(this);
chkCmd.setOnClickListener(this);
}
private void baconAndEggs() {
// TODO Auto-generated method stub
Button chkCmd = (Button) findViewById(R.id.bResults);
passTog = (ToggleButton) findViewById(R.id.tbPassword);
input = (EditText) findViewById(R.id.etCommands);
display = (TextView) findViewById(R.id.tvResults);
}
@Override
public void onClick(View view) {
// TODO Auto-generated method stub
switch (view.getId()) {
case R.id.bResults:
// TODO Auto-generated method stub
String check = input.getText().toString();
if (check.contentEquals("left") || check.contentEquals("Left")) {
display.setText("LEFT!!!");
display.setGravity(Gravity.LEFT);
} else if (check.contentEquals("center")
|| check.contentEquals("Center")) {
display.setText(check + " !!!");
display.setGravity(Gravity.CENTER);
} else if (check.contentEquals("right")
|| check.contentEquals("Right")) {
display.setText("Ha Ha you have been tricked");
display.setGravity(Gravity.LEFT);
} else if (check.contentEquals("blue")
|| check.contentEquals("Blue")) {
display.setText("Yellow");
display.setTextColor(Color.BLUE);
display.setTextSize(50);
} else if (check.contentEquals("role")) {
// Random
// Way one (created by me) Num choser
/*
* double joe = Math.random(); double crazy = joe *100; int cast
* = (int) crazy; display.setText("" + cast);
*/
// Way two Travis
Random crazyRandom = new Random();
display.setText("Dice???????!!!!!!");
display.setTextSize(crazyRandom.nextInt(200));
display.setTextColor(Color.rgb(crazyRandom.nextInt(256),
crazyRandom.nextInt(256), crazyRandom.nextInt(256)));
// Switch Travis
switch (crazyRandom.nextInt(3)) {
case 0:
display.setGravity(Gravity.LEFT);
break;
case 1:
display.setGravity(Gravity.CENTER);
break;
case 2:
display.setGravity(Gravity.RIGHT);
break;
}
} else if (check.contentEquals("cat")) {
MediaPlayer meow = MediaPlayer
.create(TextPlay.this, R.raw.meow);
display.setText("I am the most evil cat");
meow.start();
} else {
display.setGravity(Gravity.CENTER);
display.setTextColor(Color.WHITE);
display.setText("invalid");
}
break;
case R.id.tbPassword:
if (passTog.isChecked() == true) {
input.setInputType(InputType.TYPE_CLASS_TEXT
| InputType.TYPE_TEXT_VARIATION_PASSWORD);
} else {
input.setInputType(InputType.TYPE_CLASS_TEXT);
}
break;
}
}
}
最后
package com.infitenothing.word;
import com.infitenothing.dogcat.R;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.RadioGroup;
import android.widget.RadioGroup.OnCheckedChangeListener;
import android.widget.TextView;
public class OpenClass extends Activity implements OnClickListener,
OnCheckedChangeListener {
TextView question, test;
Button returnData;
RadioGroup selectList;
String gotBread;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.send);
intalize();
/*
* Bundle gotBasket = getIntent().getExtras(); gotBread =
* gotBasket.getString("key"); question.setText(gotBread);
*/
}
private void intalize() {
// TODO Auto-generated method stub
question = (TextView) findViewById(R.id.tvQestion);
question = (TextView) findViewById(R.id.tvQestion);
selectList = (RadioGroup) findViewById(R.id.rgAnswer);
selectList.setOnCheckedChangeListener(this);
returnData.setOnClickListener(this);
}
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
}
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
// TODO Auto-generated method stub
// check id ma not be right vabable
switch (checkedId) {
case R.id.rcrazy:
break;
case R.id.rawsome:
break;
case R.id.rBoth:
break;
}
}
}
我的 Android list 是
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.infitenothing.dogcat"
android:versionCode="1"
android:versionName="1.0009" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="18" />
<permission android:name="android.permission.SET_WALLPAPER"></permission>
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.infitenothing.word.MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="com.infitenothing.word.MainActivity" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
<activity
android:name="com.infitenothing.word.Menu"
android:label="SPLASH" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name="com.infitenothing.word.Splash"
android:label="SPLASH" >
<intent-filter>
<action android:name="com.infitenothing.word.Splash" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name="com.infitenothing.word.Magic"
android:label="@string/app_name" >
<intent-filter>
<action android:name="com.infitenothing.word.Magic" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
<activity
android:name="com.infitenothing.word.TextPlay"
android:label="@string/app_name" >
</activity>
<activity
android:name="com.infitenothing.word.Email"
android:label="@string/app_name" >
</activity>
<activity
android:name="com.infitenothing.word.Camera"
android:label="Camera to Wallpaper"
android:screenOrientation="portrait"
>
</activity>
<activity
android:name="com.infitenothing.word.Data"
android:label="Camera to Wallpaper"
android:screenOrientation="portrait"
>
<intent-filter>
<action android:name="com.infitenothing.word.Data" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
<activity
android:name="com.infitenothing.word.OpenClass"
android:label="Camera to Wallpaper"
android:screenOrientation="portrait"
>
</activity>
</application>
</manifest>
很抱歉这篇文章太长了,但我已经四处寻找了一段时间,但没有找到任何好的答案来解决这个问题。
任何解决此问题的答案都会有很大帮助
最佳答案
您正在按钮上调用 setOnClickListener 方法:
returnData.setOnClickListener(this);
但您以前从未初始化过此按钮! ==> NPE
要解决此问题,您需要像处理其他 View 一样使用 findViewById:
returnData = (Button) findViewById(R.id.the_id_of_your_button);
关于java - 获取错误致命 java.lang.RuntimeException : Unable to start activity,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22392815/
我需要您在以下方面提供帮助。近一个月来,我一直在阅读有关任务和异步的内容。 我想尝试在一个简单的 wep api 项目中实现我新获得的知识。我有以下方法,并且它们都按预期工作: public Htt
我的可执行 jar 中有一个模板文件 (.xls)。不需要在运行时我需要为这个文件创建 100 多个副本(稍后将唯一地附加)。用于获取 jar 文件中的资源 (template.xls)。我正在使用
我在查看网站的模型代码时对原型(prototype)有疑问。我知道这对 Javascript 中的继承很有用。 在这个例子中... define([], function () { "use
影响我性能的前三项操作是: 获取滚动条 获取偏移高度 Ext.getStyle 为了解释我的应用程序中发生了什么:我有一个网格,其中有一列在每个单元格中呈现网格。当我几乎对网格的内容做任何事情时,它运
我正在使用以下函数来获取 URL 参数。 function gup(name, url) { name = name.replace(/[\[]/, '\\\[').replace(/[\]]/,
我最近一直在使用 sysctl 来做很多事情,现在我使用 HW_MACHINE_ARCH 变量。我正在使用以下代码。请注意,当我尝试获取其他变量 HW_MACHINE 时,此代码可以完美运行。我还认为
关闭。这个问题不符合Stack Overflow guidelines .它目前不接受答案。 关闭 9 年前。 要求提供代码的问题必须表现出对所解决问题的最低限度的理解。包括尝试过的解决方案、为什么
由于使用 main-bower-files 作为使用 Gulp 的编译任务的一部分,我无法使用 node_modules 中的 webpack 来require 模块code> dir 因为我会弄乱当
关闭。这个问题需要更多focused .它目前不接受答案。 想改进这个问题吗? 更新问题,使其只关注一个问题 editing this post . 关闭 5 年前。 Improve this qu
我使用 Gridlayout 在一行中放置 4 个元素。首先,我有一个 JPanel,一切正常。对于行数变大并且我必须能够向下滚动的情况,我对其进行了一些更改。现在我的 JPanel 上添加了一个 J
由于以下原因,我想将 VolumeId 的值保存在变量中: #!/usr/bin/env python import boto3 import json import argparse import
我正在将 MSAL 版本 1.x 更新为 MSAL-browser 的 Angular 。所以我正在尝试从版本 1.x 迁移到 2.X.I 能够成功替换代码并且工作正常。但是我遇到了 acquireT
我知道有很多关于此的问题,例如 Getting daily averages with pandas和 How get monthly mean in pandas using groupby但我遇到
This is the query string that I am receiving in URL. Output url: /demo/analysis/test?startDate=Sat+
我正在尝试使用 javascript 中的以下代码访问 Geoserver 层 var gkvrtWmsSource =new ol.source.ImageWMS({ u
API 需要一个包含授权代码的 header 。这就是我到目前为止所拥有的: var fullUrl = 'https://api.ecobee.com/1/thermostat?json=\{"s
如何获取文件中的最后一个字符,如果是某个字符,则删除它而不将整个文件加载到内存中? 这就是我目前所拥有的。 using (var fileStream = new FileStream("file.t
我是这个社区的新手,想出了我的第一个问题。 我正在使用 JSP,我成功地创建了 JSP-Sites,它正在使用jsp:setParameter 和 jsp:getParameter 具有单个字符串。
在回答 StoreStore reordering happens when compiling C++ for x86 @Peter Cordes 写过 For Acquire/Release se
我有一个函数,我们将其命名为 X1,它返回变量 Y。该函数在操作 .on("focusout", X1) 中使用。如何获取变量Y?执行.on后X1的结果? 最佳答案 您可以更改 Y 的范围以使其位于函
我是一名优秀的程序员,十分优秀!