简体   繁体   English

java.lang.RuntimeException:无法实例化活动

[英]java.lang.RuntimeException: Unable to instantiate activity

I am developing an application to display records count of shape file in android. 我正在开发一个应用程序来显示android中形状文件的记录数。 But it giving null pointer exception due to following errors 但是由于以下错误,它给出了空指针异常

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

And SkeletonActivity.java is 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(); 

            }
}

And Manifest.xml is 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>

Please help me to resolve this problem. 请帮助我解决此问题。 Thanks. 谢谢。

your Manifest file should be 您的清单文件应为

<?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>

The problem is your main intent (SkeletonActivity) has only 1 constructor, which android have no idea what todo. 问题是您的主要意图(SkeletonActivity)只有1个构造函数,而android不知道该怎么做。 Start this activity via another or remove the constructor SkeletonActivity(Context context) 通过另一个启动此活动或删除构造函数SkeletonActivity(Context context)

Please check your Android manifest file. 请检查您的Android清单文件。 it seems to be incorrect. 这似乎是不正确的。 try to remove the comments from the manifest file then try. 尝试从清单文件中删除注释,然后尝试。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM