java.lang.RuntimeException: 无法启动活动 ComponentInfo java.lang.nullpointerexception 新手代码
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18748432/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
java.lang.RuntimeException: Unable to start activity ComponentInfo java.lang.nullpointerexception newbie code
提问by user2769831
I'm very new to programming and I'm sorry if this type of question has been asked tons of times already. I'm really very new to all this and reading code is still quite confusing to me.
我对编程很陌生,如果此类问题已经被问过很多次,我很抱歉。我对这一切真的很陌生,阅读代码对我来说仍然很困惑。
So I've been following some tutorials online and I've managed to create this simple app that adds or subtracts 1 depending on the button pressed.
所以我一直在网上学习一些教程,我设法创建了这个简单的应用程序,它根据按下的按钮增加或减少 1。
I've recently picked up how to add a splash screen and learning how activities and intents work.
我最近学习了如何添加启动画面并学习活动和意图的工作方式。
So I tried adding a splash art before my app starts, but I get an error that crashes the app after the splash art ends. I was wondering if you could help a newbie out. thanks in advance. cheers!
所以我尝试在我的应用程序启动之前添加一个启动画面,但是我收到一个错误,在启动画面结束后我的应用程序崩溃。我想知道你是否可以帮助一个新手。提前致谢。干杯!
this is the error from logcat
这是 logcat 的错误
09-11 14:02:29.312: D/dalvikvm(3078): GC_FOR_ALLOC freed 62K, 10% free 2676K/2948K, paused 29ms,
total 31ms
09-11 14:02:29.382: I/dalvikvm-heap(3078): Grow heap (frag case) to 12.189MB for 9830416-byte
allocation
09-11 14:02:29.422: D/dalvikvm(3078): GC_FOR_ALLOC freed 2K, 3% free 12274K/12552K, paused 44ms,
total 44ms
09-11 14:02:30.202: D/dalvikvm(3078): GC_FOR_ALLOC freed <1K, 3% free 12274K/12552K, paused
26ms, total 26ms
09-11 14:02:30.313: I/dalvikvm-heap(3078): Grow heap (frag case) to 28.857MB for 17479696-byte
allocation
09-11 14:02:30.432: D/dalvikvm(3078): GC_FOR_ALLOC freed <1K, 1% free 29343K/29624K, paused
117ms, total 117ms
09-11 14:02:31.052: D/gralloc_goldfish(3078): Emulator without GPU emulation detected.
09-11 14:02:34.112: D/AndroidRuntime(3078): Shutting down VM
09-11 14:02:34.124: W/dalvikvm(3078): threadid=1: thread exiting with uncaught exception
(group=0x41465700)
09-11 14:02:34.152: E/AndroidRuntime(3078): FATAL EXCEPTION: main
09-11 14:02:34.152: E/AndroidRuntime(3078): java.lang.RuntimeException: Unable to start activity
ComponentInfo{com.testapp.numbercounter/com.testapp.numbercounter.Main}:
java.lang.NullPointerException
09-11 14:02:34.152: E/AndroidRuntime(3078): at
android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2211)
09-11 14:02:34.152: E/AndroidRuntime(3078): at
android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2261)
09-11 14:02:34.152: E/AndroidRuntime(3078): at
android.app.ActivityThread.access0(ActivityThread.java:141)
09-11 14:02:34.152: E/AndroidRuntime(3078): at
android.app.ActivityThread$H.handleMessage(ActivityThread.java:1256)
09-11 14:02:34.152: E/AndroidRuntime(3078): at
android.os.Handler.dispatchMessage(Handler.java:99)
09-11 14:02:34.152: E/AndroidRuntime(3078): at android.os.Looper.loop(Looper.java:137)
09-11 14:02:34.152: E/AndroidRuntime(3078): at
android.app.ActivityThread.main(ActivityThread.java:5103)
09-11 14:02:34.152: E/AndroidRuntime(3078):at
java.lang.reflect.Method.invokeNative(NativeMethod)
09-11 14:02:34.152: E/AndroidRuntime(3078): at
java.lang.reflect.Method.invoke(Method.java:525)
09-11 14:02:34.152: E/AndroidRuntime(3078): at
com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
09-11 14:02:34.152: E/AndroidRuntime(3078): at
com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
09-11 14:02:34.152: E/AndroidRuntime(3078): at dalvik.system.NativeStart.main(Native Method)
09-11 14:02:34.152: E/AndroidRuntime(3078): Caused by: java.lang.NullPointerException
09-11 14:02:34.152: E/AndroidRuntime(3078): at android.graphics.PorterDuffColorFilter.<init>
(PorterDuffColorFilter.java:28)
09-11 14:02:34.152: E/AndroidRuntime(3078): at
android.graphics.drawable.Drawable.setColorFilter(Drawable.java:424)
09-11 14:02:34.152: E/AndroidRuntime(3078): at
com.testapp.numbercounter.Main.onCreate(Main.java:26)
09-11 14:02:34.152: E/AndroidRuntime(3078): at
android.app.Activity.performCreate(Activity.java:5133)
09-11 14:02:34.152: E/AndroidRuntime(3078): at
android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
09-11 14:02:34.152: E/AndroidRuntime(3078): at
android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2175)
09-11 14:02:34.152: E/AndroidRuntime(3078): ... 11 more
09-11 14:02:38.182: I/Process(3078): Sending signal. PID: 3078 SIG: 9
Here's the code to the Main.class
这是 Main.class 的代码
import android.R.color;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class Main extends Activity {
TextView number;
Button add, sub, res;
int counter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
counter = 0;
number = (TextView) findViewById(R.id.numberTxt);
add = (Button) findViewById(R.id.addBtn);
sub = (Button) findViewById(R.id.subBtn);
res = (Button) findViewById(R.id.resBtn);
add.getBackground().setColorFilter(color.white, null);
sub.getBackground().setColorFilter(color.white, null);
res.getBackground().setColorFilter(color.white, null);
add.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
counter++;
number.setText(Integer.toString(counter));
}
});
sub.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
counter--;
number.setText(Integer.toString(counter));
}
});
res.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
counter = 0;
number.setText(Integer.toString(counter));
}
});
}
@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;
}
}
and here's the code to the SplashActivity.class
这是 SplashActivity.class 的代码
package com.testapp.numbercounter;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
public class SplashActivity extends Activity {
@Override
protected void onCreate(Bundle SplashBndl) {
// TODO Auto-generated method stub
super.onCreate(SplashBndl);
setContentView(R.layout.splashscreen);
Thread timer = new Thread(){
public void run(){
try{
sleep(3000);
} catch (InterruptedException e){
e.printStackTrace();
}
finally {
Intent StartPoint = new Intent(SplashActivity.this, Main.class);
SplashActivity.this.startActivity(StartPoint);
}
}
};
timer.start();
}
protected void onPause(){
super.onPause();
finish();
}
}
and finally my AndroidManifest.xml
最后是我的 AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.testapp.numbercounter"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="14"
android:targetSdkVersion="17" />
<application
android:allowBackup="true" android:icon="@drawable/ic_launcher"
android:label="@string/app_name" android:theme="@style/AppTheme" >
<activity
android:name=".SplashActivity" android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".Main" android:label="@string/app_name" >
<intent-filter>
<action android:name="com.testapp.numbercounter.MAIN" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
</application>
</manifest>
hope someone can help I've been stuck on this problem for a while now.
希望有人可以帮助我已经被这个问题困住了一段时间。
采纳答案by dymmeh
add.getBackground().setColorFilter(color.white, null);
You cannot provide a null
PorterDuff mode (The second argument) when setting the color filter
null
设置滤色器时不能提供PorterDuff 模式(第二个参数)
Specify a porterduff mode and it should work as long as everything else in your code is functional..
指定 porterduff 模式,只要代码中的其他所有内容都可以正常工作,它就应该可以工作。
add.getBackground().setColorFilter(Color.white, PorterDuff.Mode.SRC_ATOP); //pick whatever PorterDuff mode is appropriate for you here
I'm not sure what you're trying to accomplish with the colorfilter. If all you want is to specify a background color you can just use
我不确定你想用滤色器完成什么。如果您只想指定背景颜色,您可以使用
add.setBackgroundColor(Color.white);
回答by crazy
<activity
android:name=".Main" android:label="@string/app_name" >
<intent-filter>
<action android:name="com.testapp.numbercounter.MAIN" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
remove the <intent-filter
> from the <activity>
tag..
<intent-filter
从<activity>
标签中删除> ..
回答by khubaib
Problem is not with the activity in manifest. This is a null pointer in this line
问题不在于清单中的活动。这是这一行中的空指针
add.getBackground().setColorFilter(color.white, null);
check the answer given by dymmeh for more info.
检查 dymmeh 给出的答案以获取更多信息。