Java错误:无法启动活动ComponentInfo:Java.lang.NullPoInterException



我一直面临着关于 java.lng.nullpointerexeption 的这种错误。

main.java 包com.example.shoppingassistantnew;

import greendroid.app.GDActivity;
import greendroid.widget.ActionBarItem;
import greendroid.widget.ActionBarItem.Type;

import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.MenuItem;
import android.support.v4.app.NavUtils;
public class main extends GDActivity {

    private static final int EXPAND = 0;
    private static final int ADD = 1;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
       setActionBarContentView(R.layout.shoppinglist);
        initActionBar();
    }
    private void initActionBar(){
        addActionBarItem(Type.List,EXPAND);
        addActionBarItem(Type.Add,ADD);
    }

     @Override
        public boolean onHandleActionBarItemClick(ActionBarItem item, int position) {
            // TODO Auto-generated method stub
         switch(item.getItemId()){
         case ADD:
             Intent intent=new Intent(main.this,AddShoppingList.class);
             startActivity(intent);
         }
            return super.onHandleActionBarItemClick(item, position);
        }
     static final class ProductData{
            String barcode;
            String format;
        }
}

addshoppinglist.java

package com.example.shoppingassistantnew;
import com.cyrilmottier.android.greendroid.R;
import com.example.shoppingassistantnew.main.ProductData;

import android.app.ActionBar.LayoutParams;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import greendroid.app.GDActivity;
public class AddShoppingList extends Activity implements OnClickListener {
    private static final ProductData mProductData = new ProductData();
    private Button mSaveBttn;
    EditText mBarcodeEdit;
    EditText mFormatEdit;
    ProductDatabase mProductDb;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.addshoppinglist);
        mBarcodeEdit = (EditText) findViewById(R.id.shoppingListNameEdit);
        mFormatEdit = (EditText) findViewById(R.id.shoppingDateEdit);
        mSaveBttn.setOnClickListener(this);

    }

    private void showInfoDialog(Context context, String title,
            String information) {
        new AlertDialog.Builder(context).setMessage(information)
                .setTitle(title)
                .setPositiveButton("OK", new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int which) {
                        dialog.dismiss();
                    }
                }).show();
    }

    public void onClick(View v) {
        // TODO Auto-generated method stub
    }
}

productdatabase.java

package com.example.shoppingassistantnew;
import com.example.shoppingassistantnew.main.ProductData;
import android.content.ContentValues;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;
public class ProductDatabase  {
private static final String PRODUCT_TABLE="products";
private static final String DATABASE_NAME="spot_pay.db";
private static final int DATABASE_VERSION=1;
private SQLiteDatabase db;

private static class ProductDatabaseHelper extends SQLiteOpenHelper {
    private static final String TAG = null;
    public ProductDatabaseHelper(Context context) {
        super(context, DATABASE_NAME, null, DATABASE_VERSION);
        // TODO Auto-generated constructor stub
    }
    @Override
    public void onCreate(SQLiteDatabase db) {
        // TODO Auto-generated method stub
        StringBuilder sql=new StringBuilder();
        sql.append("create table ").append(PRODUCT_TABLE)
            .append("(  ")
            .append("   _id integer primary key,")
            .append("   barcode text,")
            .append("   format text,")
          .append(")  ");

        db.execSQL(sql.toString());
          Log.d(TAG, PRODUCT_TABLE + "table created");    
    }
    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        // TODO Auto-generated method stub
         db.execSQL("drop table if exists " + PRODUCT_TABLE);                    
            onCreate(db);
    }

}

public ProductDatabase(Context context){
     ProductDatabaseHelper helper = new ProductDatabaseHelper(context);
  db = helper.getWritableDatabase();
}
public boolean insert(ProductData product){
    ContentValues vals = new ContentValues();
      vals.put("barcode", product.barcode);
        vals.put("format", product.format);
     return db.insert(PRODUCT_TABLE, null, vals) != -1;
}
}

androidmanifest.xml

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.shoppingassistantnew"
    android:versionCode="1"
    android:versionName="1.0" >
    <uses-sdk
        android:minSdkVersion="7"
        android:targetSdkVersion="15" />
    <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
         android:theme="@style/Theme.GDActionBarExample"
            android:name=".GDIntroApp">
        <activity
            android:name=".main"
            android:label="@string/title_activity_main" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity android:name=".AddShoppingList" android:theme="@style/myDialog"></activity>
    </application>
</manifest>

logcat

10-27 10:18:15.545: D/AndroidRuntime(1633): Shutting down VM
10-27 10:18:15.545: W/dalvikvm(1633): threadid=1: thread exiting with uncaught exception (group=0x4001d800)
10-27 10:18:15.555: E/AndroidRuntime(1633): FATAL EXCEPTION: main
10-27 10:18:15.555: E/AndroidRuntime(1633): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.shoppingassistantnew/com.example.shoppingassistantnew.AddShoppingList}: java.lang.NullPointerException
10-27 10:18:15.555: E/AndroidRuntime(1633):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
10-27 10:18:15.555: E/AndroidRuntime(1633):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
10-27 10:18:15.555: E/AndroidRuntime(1633):     at android.app.ActivityThread.access$2300(ActivityThread.java:125)
10-27 10:18:15.555: E/AndroidRuntime(1633):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
10-27 10:18:15.555: E/AndroidRuntime(1633):     at android.os.Handler.dispatchMessage(Handler.java:99)
10-27 10:18:15.555: E/AndroidRuntime(1633):     at android.os.Looper.loop(Looper.java:123)
10-27 10:18:15.555: E/AndroidRuntime(1633):     at android.app.ActivityThread.main(ActivityThread.java:4627)
10-27 10:18:15.555: E/AndroidRuntime(1633):     at java.lang.reflect.Method.invokeNative(Native Method)
10-27 10:18:15.555: E/AndroidRuntime(1633):     at java.lang.reflect.Method.invoke(Method.java:521)
10-27 10:18:15.555: E/AndroidRuntime(1633):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
10-27 10:18:15.555: E/AndroidRuntime(1633):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
10-27 10:18:15.555: E/AndroidRuntime(1633):     at dalvik.system.NativeStart.main(Native Method)
10-27 10:18:15.555: E/AndroidRuntime(1633): Caused by: java.lang.NullPointerException
10-27 10:18:15.555: E/AndroidRuntime(1633):     at com.example.shoppingassistantnew.AddShoppingList.onCreate(AddShoppingList.java:39)
10-27 10:18:15.555: E/AndroidRuntime(1633):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
10-27 10:18:15.555: E/AndroidRuntime(1633):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
10-27 10:18:15.555: E/AndroidRuntime(1633):     ... 11 more
10-27 10:18:17.825: I/Process(1633): Sending signal. PID: 1633 SIG: 9

AddShoppingList.java , mSaveBttn is null中首先从xml提供递减,然后使用

    mFormatEdit = (EditText) findViewById(R.id.shoppingDateEdit);
    mSaveBttn =(Button)findViewById(R.id.mSaveBttn);<<<< Give Refrence from xml
    mSaveBttn.setOnClickListener(this);
    ^^^^^^^^^

相关内容

  • 没有找到相关文章

最新更新