0

我对编程很陌生,如果这类问题已经被问了很多次,我很抱歉。我对这一切真的很陌生,阅读代码对我来说仍然很困惑。

因此,我一直在关注一些在线教程,并设法创建了这个简单的应用程序,该应用程序根据按下的按钮加减 1。

我最近学习了如何添加启动屏幕并学习活动和意图的工作方式。

所以我尝试在我的应用程序启动之前添加一个启动画面,但我收到一个错误,导致启动画面结束后应用程序崩溃。我想知道你是否可以帮助一个新手。提前致谢。干杯!

这是来自 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.access$600(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

这是 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;
}

}

这是 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();
 }


}

最后是我的 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>

希望有人可以帮助我已经被这个问题困扰了一段时间。

4

3 回答 3

2
add.getBackground().setColorFilter(color.white, null);

设置颜色过滤器时不能提供nullPorterDuff 模式(第二个参数)

指定一个 porterduff 模式,只要您的代码中的其他所有内容都可以正常工作,它就应该工作。

add.getBackground().setColorFilter(Color.white, PorterDuff.Mode.SRC_ATOP); //pick whatever PorterDuff mode is appropriate for you here

我不确定您要使用滤色器完成什么。如果您只想指定背景颜色,则可以使用

add.setBackgroundColor(Color.white);
于 2013-09-11T18:27:12.190 回答
0

问题不在于清单中的活动。这是此行中的空指针

add.getBackground().setColorFilter(color.white, null);

检查 dymmeh 给出的答案以获取更多信息。

于 2013-09-11T18:46:21.017 回答
0
<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>

从标签中删除<intent-filter> ..<activity>

于 2013-09-11T18:32:05.813 回答