因此,我已经构建了这个简单的应用程序,作为我正在学习的自我指导学习android类的一部分。直到我对Java文件进行了一些更改,然后尝试重新安装并重新启动后,它的工作情况都很好。根据我所做的研究,我认为它与清单有关,但是我不确定这是什么错误。如果您需要查看其他内容,请询问。我真的很为难。在添加switch语句之前,它一直在工作。它曾经是一个if else集群,并被告知要尝试使用switch statement进行。它似乎在Eclipse中签出,但我无法弄清楚。这是logcat的输出。


  01-22 07:43:12.106:E / libprocessgroup(1717):制作失败
  / acct / uid_10054:只读文件系统01-22 07:43:12.106:
  W / Zygote(1717):createProcessGroup失败,缺少内核
  CONFIG_CGROUP_CPUACCT? 01-22 07:43:12.107:I / art(1717):不
  后期启用-Xcheck:jni(已启用)01-22 07:43:12.192:
  D / AndroidRuntime(1717):关闭VM 01-22 07:43:12.192:
  D / AndroidRuntime(1717):---------崩溃开始01-22
  07:43:12.193:E / AndroidRuntime(1717):致命意外:主要01-22
  07:43:12.193:E / AndroidRuntime(1717):进程:
  com.codeherenow.trafficlights,PID:1717 01-22 07:43:12.193:
  E / AndroidRuntime(1717):java.lang.RuntimeException:无法执行
  实例化活动
  ComponentInfo {com.codeherenow.trafficlights / com.codeherenow.trafficlights.TrafficLightsActivity}:
  java.lang.InstantiationException:类
  com.codeherenow.trafficlights.TrafficLightsActivity不能为
  实例化01-22 07:43:12.193:E / AndroidRuntime(1717):at
  android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2209)
  01-22 07:43:12.193:E / AndroidRuntime(1717):at
  android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2360)
  01-22 07:43:12.193:E / AndroidRuntime(1717):at
  android.app.ActivityThread.access $ 800(ActivityThread.java:144)01-22
  07:43:12.193:E / AndroidRuntime(1717):at
  android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1278)
  01-22 07:43:12.193:E / AndroidRuntime(1717):at
  android.os.Handler.dispatchMessage(Handler.java:102)01-22
  07:43:12.193:E / AndroidRuntime(1717):at
  android.os.Looper.loop(Looper.java:135)01-22 07:43:12.193:
  E / AndroidRuntime(1717):在
  android.app.ActivityThread.main(ActivityThread.java:5221)01-22
  07:43:12.193:E / AndroidRuntime(1717):at
  java.lang.reflect.Method.invoke(本机方法)01-22 07:43:12.193:
  E / AndroidRuntime(1717):在
  java.lang.reflect.Method.invoke(Method.java:372)01-22 07:43:12.193:
  E / AndroidRuntime(1717):在
  com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:899)
  01-22 07:43:12.193:E / AndroidRuntime(1717):at
  com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)01-22
  07:43:12.193:E / AndroidRuntime(1717):原因:
  java.lang.InstantiationException:类
  com.codeherenow.trafficlights.TrafficLightsActivity不能为
  实例化01-22 07:43:12.193:E / AndroidRuntime(1717):at
  java.lang.Class.newInstance(Class.java:1553)01-22 07:43:12.193:
  E / AndroidRuntime(1717):在
  android.app.Instrumentation.newActivity(Instrumentation.java:1065)
  01-22 07:43:12.193:E / AndroidRuntime(1717):at
  android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2199)
  01-22 07:43:12.193:E / AndroidRuntime(1717):...还有10多个01-22
  07:48:13.660:I / Process(1717):发送信号PID:1717 SIG:9


这是清单:

    <?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.codeherenow.trafficlights"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="17" />

    <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name">
        <activity
            android:name="com.codeherenow.trafficlights.TrafficLightsActivity"
            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>

</manifest>


这是java:

package com.codeherenow.trafficlights;


import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;

public abstract class TrafficLightsActivity extends Activity implements OnClickListener {

        private ImageView redLight;
        private ImageView yellowLight;
        private ImageView greenLight;

        private Button redButton;
        private Button yellowButton;
        private Button greenButton;



    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.traffic_lights);

        redLight=(ImageView) findViewById(R.id.red_light);
        yellowLight=(ImageView) findViewById(R.id.yellow_light);
        greenLight=(ImageView) findViewById(R.id.green_light);


        redButton=(Button) findViewById(R.id.Red_Button);
        yellowButton=(Button) findViewById(R.id.Yellow_Button);
        greenButton=(Button) findViewById(R.id.Green_Button);


        redButton.setOnClickListener(this);
        yellowButton.setOnClickListener(this);
        greenButton.setOnClickListener(this);
        redLight.setOnClickListener(this);
        yellowLight.setOnClickListener(this);
        greenLight.setOnClickListener(this);


    }



    public void onClick (View v) {
        int v2=v.getId();
         turnOffLight();

        // TODO Auto-generated method stub
        switch(v2){
        case R.id.Red_Button:case R.id.red_light:
            turnOnRedLight();
            break;

        case R.id.Yellow_Button: case R.id.yellow_light:
            turnOnYellowLight();
            break;

        case R.id.Green_Button: case R.id.green_light:
            turnOnGreenLight();
            break;

            default:
            break;


        }

    }



    private void turnOffLight() {
        // TODO Auto-generated method stub
        redLight.setImageResource(R.drawable.light_off);
        greenLight.setImageResource(R.drawable.light_off);
        yellowLight.setImageResource(R.drawable.light_off);
    }





    private void turnOnGreenLight() {
        // TODO Auto-generated method stub
        greenLight.setImageResource(R.drawable.green_on);
    }



    private void turnOnYellowLight() {
        // TODO Auto-generated method stub
        yellowLight.setImageResource(R.drawable.yellow_on);
    }



    private void turnOnRedLight() {
        // TODO Auto-generated method stub
        redLight.setImageResource(R.drawable.red_on);
    }

}

最佳答案

public abstract class TrafficLightsActivity


并且您的错误是Caused by: java.lang.InstantiationException: ..

解决方案是

public class TrafficLightsActivity

10-07 12:28