1

在运行以下代码时,我收到错误消息,指出应用程序已意外停止......在我的 logcat 中......它显示了很多错误......我还附加了 logcat 错误,我也有致命异常......请帮忙。 ...

XMLdemo.java

package com.example.myfirstapplication;

import java.io.IOException;    
import java.io.InputStream;    
import java.util.ArrayList;  
import java.util.Iterator;  

import org.xmlpull.v1.XmlPullParser;  
import org.xmlpull.v1.XmlPullParserException;  
import org.xmlpull.v1.XmlPullParserFactory;  

import android.app.Activity;  
import android.os.Bundle;  
import android.view.Menu;  
import android.widget.TextView;  

class Product
{

public String name;
public String quantity;
public String color;

  }
  public class XMLDemo extends Activity {

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

    XmlPullParserFactory pullParserFactory;
    try {
        pullParserFactory = XmlPullParserFactory.newInstance();
        XmlPullParser parser = pullParserFactory.newPullParser();

            InputStream in_s = getApplicationContext().getAssets().open("temp.xml");
            parser.setFeature(XmlPullParser.FEATURE_PROCESS_NAMESPACES, false);
            parser.setInput(in_s, null);

            parseXML(parser);

    } catch (XmlPullParserException e) {

        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

}

private void parseXML(XmlPullParser parser) throws XmlPullParserException,IOException
{
    ArrayList<Product> products = null;
    int eventType = parser.getEventType();
    Product currentProduct = null;

    while (eventType != XmlPullParser.END_DOCUMENT){
        String name = null;
        switch (eventType){
            case XmlPullParser.START_DOCUMENT:
                products = new ArrayList();
                break;
            case XmlPullParser.START_TAG:
                name = parser.getName();
                if (name == "product"){
                    currentProduct = new Product();
                } else if (currentProduct != null){
                    if (name == "productname"){
                        currentProduct.name = parser.nextText();
                    } else if (name == "productcolor"){
                        currentProduct.color = parser.nextText();
                    } else if (name == "productquantity"){
                        currentProduct.quantity= parser.nextText();
                    }  
                }
                break;
            case XmlPullParser.END_TAG:
                name = parser.getName();
                if (name.equalsIgnoreCase("product") && currentProduct != null){
                    products.add(currentProduct);
                } 
        }
        eventType = parser.next();
    }

    printProducts(products);
}



private void printProducts(ArrayList<Product> products)
{
    String content = "";
    Iterator<Product> it = products.iterator();
    while(it.hasNext())
    {
        Product currProduct  = it.next();
        content = content + "nnnProduct :" +  currProduct.name + "n";
        content = content + "Quantity :" +  currProduct.quantity + "n";
        content = content + "Color :" +  currProduct.color + "n";

    }

    TextView display = (TextView)findViewById(R.id.info);
    display.setText(content);
}

@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;
}
    }

我的布局 MAIN.XML

<?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/info"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:padding="20dp"
    android:text="TextView" />

  </LinearLayout>

安卓清单.xml

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

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

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

</manifest>

运行时出现 LOGCAT 错误

09-29 19:55:21.378: D/AndroidRuntime(275): Shutting down VM
09-29 19:55:21.378: W/dalvikvm(275): threadid=1: thread exiting with uncaught exception (group=0x4001d800)
09-29 19:55:21.398: E/AndroidRuntime(275): FATAL EXCEPTION: main
09-29 19:55:21.398: E/AndroidRuntime(275): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.myfirstapplication/com.example.myfirstapplication.MainActivity}: java.lang.ClassNotFoundException: com.example.myfirstapplication.MainActivity in loader dalvik.system.PathClassLoader[/data/app/com.example.myfirstapplication-1.apk]
09-29 19:55:21.398: E/AndroidRuntime(275):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2585)
09-29 19:55:21.398: E/AndroidRuntime(275):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
09-29 19:55:21.398: E/AndroidRuntime(275):  at android.app.ActivityThread.access$2300(ActivityThread.java:125)
09-29 19:55:21.398: E/AndroidRuntime(275):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
09-29 19:55:21.398: E/AndroidRuntime(275):  at android.os.Handler.dispatchMessage(Handler.java:99)
09-29 19:55:21.398: E/AndroidRuntime(275):  at android.os.Looper.loop(Looper.java:123)
09-29 19:55:21.398: E/AndroidRuntime(275):  at android.app.ActivityThread.main(ActivityThread.java:4627)
09-29 19:55:21.398: E/AndroidRuntime(275):  at java.lang.reflect.Method.invokeNative(Native Method)
09-29 19:55:21.398: E/AndroidRuntime(275):  at java.lang.reflect.Method.invoke(Method.java:521)
09-29 19:55:21.398: E/AndroidRuntime(275):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
09-29 19:55:21.398: E/AndroidRuntime(275):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
09-29 19:55:21.398: E/AndroidRuntime(275):  at dalvik.system.NativeStart.main(Native Method)
09-29 19:55:21.398: E/AndroidRuntime(275): Caused by: java.lang.ClassNotFoundException: com.example.myfirstapplication.MainActivity in loader dalvik.system.PathClassLoader[/data/app/com.example.myfirstapplication-1.apk]
09-29 19:55:21.398: E/AndroidRuntime(275):  at dalvik.system.PathClassLoader.findClass(PathClassLoader.java:243)
09-29 19:55:21.398: E/AndroidRuntime(275):  at java.lang.ClassLoader.loadClass(ClassLoader.java:573)
09-29 19:55:21.398: E/AndroidRuntime(275):  at java.lang.ClassLoader.loadClass(ClassLoader.java:532)
09-29 19:55:21.398: E/AndroidRuntime(275):  at android.app.Instrumentation.newActivity(Instrumentation.java:1021)
09-29 19:55:21.398: E/AndroidRuntime(275):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2577)
09-29 19:55:21.398: E/AndroidRuntime(275):  ... 11 more
09-29 20:12:36.938: D/AndroidRuntime(298): Shutting down VM
09-29 20:12:36.998: W/dalvikvm(298): threadid=1: thread exiting with uncaught exception (group=0x4001d800)
09-29 20:12:37.058: E/AndroidRuntime(298): FATAL EXCEPTION: main
09-29 20:12:37.058: E/AndroidRuntime(298): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.myfirstapplication/com.example.myfirstapplication.MainActivity}: java.lang.ClassNotFoundException: com.example.myfirstapplication.MainActivity in loader dalvik.system.PathClassLoader[/data/app/com.example.myfirstapplication-1.apk]
09-29 20:12:37.058: E/AndroidRuntime(298):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2585)
09-29 20:12:37.058: E/AndroidRuntime(298):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
09-29 20:12:37.058: E/AndroidRuntime(298):  at android.app.ActivityThread.access$2300(ActivityThread.java:125)
09-29 20:12:37.058: E/AndroidRuntime(298):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
09-29 20:12:37.058: E/AndroidRuntime(298):  at android.os.Handler.dispatchMessage(Handler.java:99)
09-29 20:12:37.058: E/AndroidRuntime(298):  at android.os.Looper.loop(Looper.java:123)
09-29 20:12:37.058: E/AndroidRuntime(298):  at android.app.ActivityThread.main(ActivityThread.java:4627)
09-29 20:12:37.058: E/AndroidRuntime(298):  at java.lang.reflect.Method.invokeNative(Native Method)
09-29 20:12:37.058: E/AndroidRuntime(298):  at java.lang.reflect.Method.invoke(Method.java:521)
09-29 20:12:37.058: E/AndroidRuntime(298):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
09-29 20:12:37.058: E/AndroidRuntime(298):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
09-29 20:12:37.058: E/AndroidRuntime(298):  at dalvik.system.NativeStart.main(Native Method)
09-29 20:12:37.058: E/AndroidRuntime(298): Caused by: java.lang.ClassNotFoundException: com.example.myfirstapplication.MainActivity in loader dalvik.system.PathClassLoader[/data/app/com.example.myfirstapplication-1.apk]
09-29 20:12:37.058: E/AndroidRuntime(298):  at dalvik.system.PathClassLoader.findClass(PathClassLoader.java:243)
09-29 20:12:37.058: E/AndroidRuntime(298):  at java.lang.ClassLoader.loadClass(ClassLoader.java:573)
09-29 20:12:37.058: E/AndroidRuntime(298):  at java.lang.ClassLoader.loadClass(ClassLoader.java:532)
09-29 20:12:37.058: E/AndroidRuntime(298):  at android.app.Instrumentation.newActivity(Instrumentation.java:1021)
09-29 20:12:37.058: E/AndroidRuntime(298):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2577)
09-29 20:12:37.058: E/AndroidRuntime(298):  ... 11 more
4

1 回答 1

1

您没有在清单中提及正确的活动名称。您提到了 MainActivity。它应该是 XMLDemo。

将 com.example.myfirstapplication.MainActivity 替换为 com.example.myfirstapplication.XMLDemo 就可以了。由于清单中没有正确的活动名称,这就是它抛出 ClassNotFoundException 的原因。

于 2013-09-29T15:22:41.967 回答