Android应用程序无法启动

时间:2015-04-11 01:28:06

标签: android eclipse launch

我已经制作了这个应用程序但是当我尝试启动它时会弹出一条消息说应用程序已停止。我真的找不到代码中的任何错误。   这是代码,只有很多ImageButtons:

package com.fanis.math;

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

public class MyResistance extends Activity implements OnClickListener {
View b1, b2, b3, b4, b5, b6, b7, b8, b9, b10, b11, b12;
Button bok, bclear;
TextView t;
int k = -1;
int[] ar = new int[4];
double n = 0;
double multi=0;
String nString;

@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.resistance);

    b1 = (ImageButton) findViewById(R.id.im1);
    b2 = (ImageButton) findViewById(R.id.im2);
    b3 = (ImageButton) findViewById(R.id.im3);
    b4 = (ImageButton) findViewById(R.id.im4);
    b5 = (ImageButton) findViewById(R.id.im5);
    b6 = (ImageButton) findViewById(R.id.im6);
    b7 = (ImageButton) findViewById(R.id.im7);
    b8 = (ImageButton) findViewById(R.id.im8);
    b9 = (ImageButton) findViewById(R.id.im9);
    b10 = (ImageButton) findViewById(R.id.im10);
    b11 = (ImageButton) findViewById(R.id.im11);
    b12 = (ImageButton) findViewById(R.id.im12);
    bok = (Button) findViewById(R.id.ok);
    bclear = (Button) findViewById(R.id.clear);
    t = (TextView) findViewById(R.id.tv1);
    b1.setOnClickListener(this);
    b2.setOnClickListener(this);
    b3.setOnClickListener(this);
    b4.setOnClickListener(this);
    b5.setOnClickListener(this);
    b6.setOnClickListener(this);
    b7.setOnClickListener(this);
    b8.setOnClickListener(this);
    b9.setOnClickListener(this);
    b10.setOnClickListener(this);
    b11.setOnClickListener(this);
    b12.setOnClickListener(this);
    bok.setOnClickListener(this);
    bclear.setOnClickListener(this);

}

@Override
public void onClick(View v) {
    // TODO Auto-generated method stub
    switch (v.getId()) {
    case R.id.im1:
        k += 1;
        ar[k] = 0; // dianisma pou krataei kathe psifio
        break;
    case R.id.im2:
        k += 1;
        ar[k] = 1;
        break;
    case R.id.im3:
        k += 1;
        ar[k] = 2;
        break;
    case R.id.im4:
        k += 1;
        ar[k] = 3;
        break;
    case R.id.im5:
        k += 1;
        ar[k] = 4;
        break;
    case R.id.im6:
        k += 1;
        ar[k] = 5;
        break;
    case R.id.im7:
        k += 1;
        ar[k] = 6;
        break;
    case R.id.im8:
        k += 1;
        ar[k] = 7;
        break;
    case R.id.im9:
        k += 1;
        ar[k] = 8;
        break;
    case R.id.im10:
        k += 1;
        ar[k] = 9;
        break;
    case R.id.im11:
        k += 1;
        multi = 0.01;
        break;
    case R.id.im12:
        k += 1;
        multi = 0.1;
        break;
    case R.id.ok:
        for (int i = 1; i <= k; i++) {
            n = n + ar[i - 1] * Math.pow(10, k - i);
        }
        if (multi == 0) {
            multi = Math.pow(10, ar[k]);
        }
        n = n * multi;
        nString = String.valueOf(n) + "" + "Ω";
        t.setText(nString);
        break;
    case R.id.clear:
        t.setText("");
        n = 0;
        k = -1;
        multi=0;
    }

}

}   这就是明显的

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

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

<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <activity
        android:name=".MyResistance"
        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>

这是布局虽然我不认为这是我无法启动应用程序的原因..

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  android:orientation="vertical" >

<TextView
    android:id="@+id/tv1"
    android:layout_width="fill_parent"
    android:layout_height="70dp"
    android:gravity="center"
    android:text=""
    android:textSize="40dp" />

<LinearLayout
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:gravity="center"
    android:orientation="vertical" >

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal" >

        <ImageButton
            android:id="@+id/im1"
            android:layout_width="70dp"
            android:layout_height="70dp"
            android:src="@drawable/black" />

        <ImageButton
            android:id="@+id/im2"
            android:layout_width="70dp"
            android:layout_height="70dp"
            android:src="@drawable/brown" />

        <ImageButton
            android:id="@+id/im3"
            android:layout_width="70dp"
            android:layout_height="70dp"
            android:src="@drawable/red" />

        <ImageButton
            android:id="@+id/im4"
            android:layout_width="70dp"
            android:layout_height="70dp"
            android:src="@drawable/orange" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal" >

        <ImageButton
            android:id="@+id/im5"
            android:layout_width="70dp"
            android:layout_height="70dp"
            android:src="@drawable/yellow" />

        <ImageButton
            android:id="@+id/im6"
            android:layout_width="70dp"
            android:layout_height="70dp"
            android:src="@drawable/green" />

        <ImageButton
            android:id="@+id/im7"
            android:layout_width="70dp"
            android:layout_height="70dp"
            android:src="@drawable/blue" />

        <ImageButton
            android:id="@+id/im8"
            android:layout_width="70dp"
            android:layout_height="70dp"
            android:src="@drawable/purple" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal" >

        <ImageButton
            android:id="@+id/im9"
            android:layout_width="70dp"
            android:layout_height="70dp"
            android:src="@drawable/grey" />

        <ImageButton
            android:id="@+id/im10"
            android:layout_width="70dp"
            android:layout_height="70dp"
            android:src="@drawable/white" />

        <ImageButton
            android:id="@+id/im11"
            android:layout_width="70dp"
            android:layout_height="70dp"
            android:src="@drawable/silver" />

        <ImageButton
            android:id="@+id/im12"
            android:layout_width="70dp"
            android:layout_height="70dp"
            android:src="@drawable/gold" />
    </LinearLayout>
</LinearLayout>

<LinearLayout
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal" >

    <Button
        android:id="@+id/ok"
        android:layout_width="110dp"
        android:layout_height="wrap_content"
        android:layout_marginLeft="30dp"
        android:text="Ok" />

    <Button
        android:id="@+id/clear"
        android:layout_width="110dp"
        android:layout_height="wrap_content"
        android:layout_marginLeft="40dp"
        android:text="Clear" />
</LinearLayout>

我的logcat:

 04-11 04:29:17.420: E/AndroidRuntime(847): FATAL EXCEPTION: main
 04-11 04:29:17.420: E/AndroidRuntime(847): Process: com.fanis.math, PID: 847
04-11 04:29:17.420: E/AndroidRuntime(847): java.lang.RuntimeException:           Unable to start activity        ComponentInfo{com.fanis.math/com.fanis.math.MyResistance}:    android.view.InflateException: Binary XML file line  #44: Error inflating class  <unknown>
   04-11 04:29:17.420: E/AndroidRuntime(847):   at    android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195)
   04-11 04:29:17.420: E/AndroidRuntime(847):   at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
  04-11 04:29:17.420: E/AndroidRuntime(847):    at android.app.ActivityThread.access$800(ActivityThread.java:135)
04-11 04:29:17.420: E/AndroidRuntime(847):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
04-11 04:29:17.420: E/AndroidRuntime(847):  at android.os.Handler.dispatchMessage(Handler.java:102)
04-11 04:29:17.420: E/AndroidRuntime(847):  at android.os.Looper.loop(Looper.java:136)
04-11 04:29:17.420: E/AndroidRuntime(847):  at android.app.ActivityThread.main(ActivityThread.java:5017)
04-11 04:29:17.420: E/AndroidRuntime(847):  at java.lang.reflect.Method.invokeNative(Native Method)
04-11 04:29:17.420: E/AndroidRuntime(847):  at java.lang.reflect.Method.invoke(Method.java:515)
04-11 04:29:17.420: E/AndroidRuntime(847):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
04-11 04:29:17.420: E/AndroidRuntime(847):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
04-11 04:29:17.420: E/AndroidRuntime(847):  at dalvik.system.NativeStart.main(Native Method)
04-11 04:29:17.420: E/AndroidRuntime(847): Caused by: android.view.InflateException: Binary XML file line #44: Error inflating class <unknown>
04-11 04:29:17.420: E/AndroidRuntime(847):  at android.view.LayoutInflater.createView(LayoutInflater.java:620)
04-11 04:29:17.420: E/AndroidRuntime(847):  at com.android.internal.policy.impl.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:56)
04-11 04:29:17.420: E/AndroidRuntime(847):  at android.view.LayoutInflater.onCreateView(LayoutInflater.java:669)
04-11 04:29:17.420: E/AndroidRuntime(847):  at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:694)
04-11 04:29:17.420: E/AndroidRuntime(847):  at android.view.LayoutInflater.rInflate(LayoutInflater.java:755)
04-11 04:29:17.420: E/AndroidRuntime(847):  at android.view.LayoutInflater.rInflate(LayoutInflater.java:758)
04-11 04:29:17.420: E/AndroidRuntime(847):  at android.view.LayoutInflater.rInflate(LayoutInflater.java:758)
04-11 04:29:17.420: E/AndroidRuntime(847):  at android.view.LayoutInflater.inflate(LayoutInflater.java:492)
04-11 04:29:17.420: E/AndroidRuntime(847):  at android.view.LayoutInflater.inflate(LayoutInflater.java:397)
04-11 04:29:17.420: E/AndroidRuntime(847):  at android.view.LayoutInflater.inflate(LayoutInflater.java:353)
04-11 04:29:17.420: E/AndroidRuntime(847):  at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:290)     04-11 04:29:17.420: E/AndroidRuntime(847):     at android.app.Activity.setContentView(Activity.java:1929)
04-11 04:29:17.420: E/AndroidRuntime(847):  at com.fanis.math.MyResistance.onCreate(MyResistance.java:25)
04-11 04:29:17.420: E/AndroidRuntime(847):  at android.app.Activity.performCreate(Activity.java:5231)
04-11 04:29:17.420: E/AndroidRuntime(847):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
04-11 04:29:17.420: E/AndroidRuntime(847):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)
04-11 04:29:17.420: E/AndroidRuntime(847):  ... 11 more
04-11 04:29:17.420: E/AndroidRuntime(847): Caused by: java.lang.reflect.InvocationTargetException
04-11 04:29:17.420: E/AndroidRuntime(847):  at java.lang.reflect.Constructor.constructNative(Native Method)
04-11 04:29:17.420: E/AndroidRuntime(847):  at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
04-11 04:29:17.420: E/AndroidRuntime(847):  at android.view.LayoutInflater.createView(LayoutInflater.java:594)
04-11 04:29:17.420: E/AndroidRuntime(847):  ... 26 more
04-11 04:29:17.420: E/AndroidRuntime(847): Caused by: java.lang.OutOfMemoryError
04-11 04:29:17.420: E/AndroidRuntime(847):  at android.graphics.BitmapFactory.nativeDecodeAsset(Native Method)
04-11 04:29:17.420: E/AndroidRuntime(847):  at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:587)
04-11 04:29:17.420: E/AndroidRuntime(847):  at android.graphics.BitmapFactory.decodeResourceStream(BitmapFactory.java:422)
04-11 04:29:17.420: E/AndroidRuntime(847):  at android.graphics.drawable.Drawable.createFromResourceStream(Drawable.java:840)
04-11 04:29:17.420: E/AndroidRuntime(847):  at android.content.res.Resources.loadDrawable(Resources.java:2110)
04-11 04:29:17.420: E/AndroidRuntime(847):  at android.content.res.TypedArray.getDrawable(TypedArray.java:602)
04-11 04:29:17.420: E/AndroidRuntime(847):  at android.widget.ImageView.<init>(ImageView.java:129)
04-11 04:29:17.420: E/AndroidRuntime(847):  at android.widget.ImageButton.<init>(ImageButton.java:87)
04-11 04:29:17.420: E/AndroidRuntime(847):  at android.widget.ImageButton.<init>(ImageButton.java:83)
04-11 04:29:17.420: E/AndroidRuntime(847):  ... 29 more

1 个答案:

答案 0 :(得分:0)

更改此行:

View b1, b2, b3, b4, b5, b6, b7, b8, b9, b10, b11, b12;

为:

ImageButton b1, b2, b3, b4, b5, b6, b7, b8, b9, b10, b11, b12;