0

我正在开发一个应用程序来显示 android 中形状文件的记录数。但是由于以下错误,它给出了空指针异常

07-23 15:16:32.598: W/dalvikvm(2844): threadid=1: thread exiting with uncaught exception (group=0x4001d800)

07-23 15:16:32.677: E/AndroidRuntime(2844): FATAL EXCEPTION: main

07-23 15:16:32.677: E/AndroidRuntime(2844): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.android.skeletonapp/com.example.android.skeletonapp.SkeletonActivity}: java.lang.InstantiationException: com.example.android.skeletonapp.SkeletonActivity

07-23 15:16:32.677: E/AndroidRuntime(2844):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2585)

07-23 15:16:32.677: E/AndroidRuntime(2844):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)

07-23 15:16:32.677: E/AndroidRuntime(2844):     at android.app.ActivityThread.access$2300(ActivityThread.java:125)

07-23 15:16:32.677: E/AndroidRuntime(2844):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)

07-23 15:16:32.677: E/AndroidRuntime(2844):     at android.os.Handler.dispatchMessage(Handler.java:99)

07-23 15:16:32.677: E/AndroidRuntime(2844):     at android.os.Looper.loop(Looper.java:123)

07-23 15:16:32.677: E/AndroidRuntime(2844):     at android.app.ActivityThread.main(ActivityThread.java:4627)

07-23 15:16:32.677: E/AndroidRuntime(2844):     at java.lang.reflect.Method.invokeNative(Native Method)

07-23 15:16:32.677: E/AndroidRuntime(2844):     at java.lang.reflect.Method.invoke(Method.java:521)

07-23 15:16:32.677: E/AndroidRuntime(2844):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)

07-23 15:16:32.677: E/AndroidRuntime(2844):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
07-23 15:16:32.677: E/AndroidRuntime(2844):     at dalvik.system.NativeStart.main(Native Method)

07-23 15:16:32.677: E/AndroidRuntime(2844): Caused by: java.lang.InstantiationException: com.example.android.skeletonapp.SkeletonActivity

07-23 15:16:32.677: E/AndroidRuntime(2844):     at java.lang.Class.newInstanceImpl(Native Method)



07-23 15:16:32.677: E/AndroidRuntime(2844):     at java.lang.Class.newInstance(Class.java:1429)



07-23 15:16:32.677: E/AndroidRuntime(2844):     at android.app.Instrumentation.newActivity(Instrumentation.java:1021)



07-23 15:16:32.677: E/AndroidRuntime(2844):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2577)

07-23 15:16:32.677: E/AndroidRuntime(2844):     ... 11 more

SkeletonActivity.java 是

public class SkeletonActivity extends Activity {

    static final private int BACK_ID = Menu.FIRST;

    static final private int CLEAR_ID = Menu.FIRST + 1;

     private static String SHP_PATH =  Environment.getDataDirectory().getAbsolutePath() + "/data/com.shapes/places";

     private static String SHP_NAME = "popplaces.shp";

    private EditText mEditor;

   private final Context myContext;

    public SkeletonActivity(Context context) {
        this.myContext=context;
    }

    /** Called with the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        int recordCount = 0;

        // Inflate our UI from its XML layout description.

        setContentView(R.layout.skeleton_activity);


        // Find the text editor view inside the layout, because we

        // want to do various programmatic things with it.

        mEditor = (EditText) findViewById(R.id.editor);


        // Hook up button presses to the appropriate event handler.

        ((Button) findViewById(R.id.back)).setOnClickListener(mBackListener);

        ((Button) findViewById(R.id.clear)).setOnClickListener(mClearListener);

        mEditor.setText(getText(R.string.main_label));
        try {
            //File path = Environment.getExternalStoragePublicDirectory(
               //     Environment.DIRECTORY_PICTURES);
             //path = Environment.getDataDirectory().getAbsolutePath();
            //path.mkdirs();
            //shpFile = path.getPath().toString();
            copyFromZipFile();
             String shpFile = Environment.getDataDirectory().getAbsolutePath()+"/data/com.shapes/places/popplaces.shp";
            ShapeReader shR = new ShapeReader(shpFile, true);
                recordCount = shR.getCount(0);

        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        mEditor.setText("Record Count == " + recordCount);
    }

    /**
     * Called when the activity is about to start interacting with the user.
     */
    @Override
    protected void onResume() {
        super.onResume();
    }

    /**
     * Called when your activity's options menu needs to be created.
     */
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        super.onCreateOptionsMenu(menu);

        // We are going to create two menus. Note that we assign them
        // unique integer IDs, labels from our string resources, and
        // given them shortcuts.
        menu.add(0, BACK_ID, 0, R.string.back).setShortcut('0', 'b');
        menu.add(0, CLEAR_ID, 0, R.string.clear).setShortcut('1', 'c');

        return true;
    }

    /**
     * Called right before your activity's option menu is displayed.
     */
    @Override
    public boolean onPrepareOptionsMenu(Menu menu) {
        super.onPrepareOptionsMenu(menu);

        // Before showing the menu, we need to decide whether the clear
        // item is enabled depending on whether there is text to clear.
        menu.findItem(CLEAR_ID).setVisible(mEditor.getText().length() > 0);

        return true;
    }

    /**
     * Called when a menu item is selected.
     */
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
        case BACK_ID:
            finish();
            return true;
        case CLEAR_ID:
            mEditor.setText("");
            return true;
        }

        return super.onOptionsItemSelected(item);
    }

    /**
     * A call-back for when the user presses the back button.
     */
    OnClickListener mBackListener = new OnClickListener() {
        public void onClick(View v) {
            finish();
        }
    };

    /**
     * A call-back for when the user presses the clear button.
     */
    OnClickListener mClearListener = new OnClickListener() {
        public void onClick(View v) {
            mEditor.setText("");
        }
    };

   public void copyFromZipFile() throws IOException{ 

         InputStream is = myContext.getResources().openRawResource(R.raw.shape);

             // Path to the just created empty db 

             File outFile = new File(SHP_PATH ,SHP_NAME); 

              //Open the empty db as the output stream 

             OutputStream myOutput = new FileOutputStream(outFile.getAbsolutePath()); 

             ZipInputStream zis = new ZipInputStream(new BufferedInputStream(is)); 

              try { 

                  ZipEntry ze =  null; 

                  while ((ze = zis.getNextEntry()) != null) { 

                      ByteArrayOutputStream baos = new ByteArrayOutputStream(); 

                      byte[] buffer = new byte[24 * 1024]; 

                      int count; 

                      while ((count = zis.read(buffer)) != -1) { 

                          baos.write(buffer, 0, count); 

                          //Log.d("", buffer.toString()); 

                          baos.writeTo(myOutput); 

                          baos.reset();

                      } 
                      baos.writeTo(myOutput); 


                  } 
              } 
              catch (IOException e) {}

                  zis.close(); 

                  myOutput.flush(); 

                  myOutput.close(); 

                  is.close(); 

            }
}

Manifest.xml 是

<?xml version="1.0" encoding="utf-8"?>


<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.android.skeletonapp">


    <application android:label="@string/skeleton_app">


        <activity android:name="SkeletonActivity">


            <intent-filter>

                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>

        </activity>

    </application>

</manifest>

请帮我解决这个问题。谢谢。

4

3 回答 3

0

你的清单文件应该是

<?xml version="1.0" encoding="utf-8"?>

<application android:label="@string/skeleton_app">


    <activity android:name="SkeletonActivity">

        <!-- An IntentFilter tells the system when it should use your  -->


        <intent-filter>

            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>

    </activity>

</application>

于 2012-07-23T12:54:08.157 回答
0

问题是您的主要意图(SkeletonActivity)只有 1 个构造函数,android 不知道要做什么。通过另一个启动此活动或删除构造函数 SkeletonActivity(Context context)

于 2012-07-23T12:56:22.257 回答
-1

请检查您的 Android 清单文件。这似乎是不正确的。尝试从清单文件中删除注释,然后尝试。

于 2012-07-23T12:52:30.123 回答