无法启动活动空指针异常(在启动新意图时)

时间:2014-01-06 07:52:10

标签: android eclipse

无法开始活动。

我有一个上一页正常工作正常,但是当我调用Order_disp页面时,弹出错误并且app停止工作。 代码如下

Order_disp.java

package com.example.sopa;
import java.util.ArrayList;
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.Menu;
import android.widget.ListView;
public class Order_disp extends Activity {
    ListView orderdisp;
    ArrayList<OrderListdata> myList_Ord=new ArrayList<OrderListdata>();
    Context context;
    SQLiteHelper sqh=new SQLiteHelper(context);
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_order_disp);
        orderdisp=(ListView)findViewById(R.id.ordView1);
        getInList();
                orderdisp.setAdapter(new Badap_Ord(Order_disp.this,myList_Ord));
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.order_disp, menu);
        return true;
    }
    void getInList(){
        myList_Ord=sqh.getOrder();

    }

}


SQLiteHelper.java

package com.example.sopa;

import java.util.ArrayList;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;
import android.widget.Toast;

public class SQLiteHelper extends SQLiteOpenHelper {

    public static final String TABLE_Name = "OrderTab";
      public static final String COLUMN_ID = "id";
      public static final String COLUMN_COMMENT = "comment";
      Context context;
      private static final String DATABASE_NAME = "commments.db";
      private static final int DATABASE_VERSION = 1;
      ArrayList<OrderListdata> myList_order=new ArrayList<OrderListdata>();
      // Database creation sql statement
      private static final String DATABASE_CREATE = "create table IF NOT EXISTS "
          + TABLE_Name + "(" + COLUMN_ID
          + " integer primary key autoincrement,DishName text not null,Quant text not null,Price text not null );";

      private static final String DATABASE_CREATE1 = "create table BillTab (Id integer primary key autoincrement,DishName text not null,Quant text not null,Price text not null);";


      public SQLiteHelper(Context context) {
        super(context, DATABASE_NAME, null, DATABASE_VERSION);
      }

      @Override
      public void onCreate(SQLiteDatabase database) {
        database.execSQL(DATABASE_CREATE);
        database.execSQL(DATABASE_CREATE1);
      }

      @Override
      public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        Log.w(SQLiteHelper.class.getName(),
            "Upgrading database from version " + oldVersion + " to "
                + newVersion + ", which will destroy all old data");
        db.execSQL("DROP TABLE IF EXISTS " + TABLE_Name);
        onCreate(db);
        db.execSQL("DROP TABLE IF EXISTS BillTab");
        onCreate(db);
      }


      void addOrder(ContentValues values) {
            SQLiteDatabase db = this.getWritableDatabase();
            // Inserting Row
            db.insert("OrderTab", null, values);
            //Toast.makeText(context, "You hav entered data", Toast.LENGTH_SHORT).show();
            db.close(); // Closing database connection
        }
      ArrayList<OrderListdata> getOrder()
      {

          SQLiteDatabase db = this.getWritableDatabase();
          String cols[] = new String[] {"id","DishName","Quant","Price"};

            Cursor cur = db.query("OrderTab",cols,null,null,null,null, null);

            String result = "";

            while(cur.moveToNext()){

                OrderListdata ld=new OrderListdata();
                ld.setDname(cur.getString(1));
                ld.setPrice(cur.getString(2));
                ld.setQtyval(cur.getString(3));
                myList_order.add(ld);

            }
return myList_order;
      }


}
OrderListdata.java

package com.example.sopa;

import android.view.View;
import android.widget.NumberPicker;

public class OrderListdata {
    String dname;
    View cv;
    String qtyval;
    String price;
    public String getPrice() {
        return price;
    }
    public void setPrice(String price) {

        this.price = price;
    }
    public String getDname() {
        return dname;
    }
    public void setDname(String dname) {

        this.dname = dname;
    }
    public String getQtyval() {
        return qtyval;
    }
    public void setQtyval(String qtyval) {
        final NumberPicker np = (NumberPicker) cv.findViewById(R.id.setqty);
        np.setValue(Integer.parseInt(qtyval));
        this.qtyval = qtyval;
    }


}
activity_order_disp.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".Order_disp" >

    <ListView
        android:id="@+id/ordView1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        >
    </ListView>

</RelativeLayout>

orderdisp.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:ignore="HardcodedText,UnknownIdInLayout" >

    <Button
        android:id="@+id/remove"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBottom="@+id/Dishname"
        android:layout_alignParentRight="true"
        android:layout_marginRight="123dp"
        android:text="Remove Dish" />

    <NumberPicker
        android:id="@+id/setqty"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_marginRight="94dp"
        android:layout_toLeftOf="@+id/remove" />

    <TextView
        android:id="@+id/Dishname"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:layout_marginLeft="100dp"
        android:layout_marginTop="51dp"
        android:text="Dishname"
        android:textAppearance="?android:attr/textAppearanceLarge" />

</RelativeLayout>

Manifest.xml

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

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

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name" 
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.example.sopa.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.sopa.Main_menu"
            android:label="@string/title_activity_main_menu"
            >
        </activity>
         <activity
        android:name="com.example.sopa.DisplayMessageActivity"
        android:label="title_activity_display_message"
        android:parentActivityName="com.example.sopa.Main_menu" >
        <!-- Parent activity meta-data to support API level 7+ -->
        <meta-data
            android:name="android.support.PARENT_ACTIVITY"
            android:value="com.example.sopa.Main_menu" />
    </activity>
        <activity
            android:name="com.example.sopa.Indian"
            android:label="@string/title_activity_indian" >
        </activity>
        <activity
            android:name="com.example.sopa.Chinese"
            android:label="@string/title_activity_chinese" >
        </activity>
        <activity
            android:name="com.example.sopa.Italian"
            android:label="@string/title_activity_italian" >
        </activity>
        <activity
            android:name="com.example.sopa.Mexican"
            android:label="@string/title_activity_mexican" >
        </activity>
        <activity android:name="com.example.sopa.Order_disp"></activity>
    </application>

</manifest>

logcat的

 03-03 01:05:26.753: E/AndroidRuntime(6560): FATAL EXCEPTION: main
03-03 01:05:26.753: E/AndroidRuntime(6560): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.sopa/com.example.sopa.Order_disp}: java.lang.NullPointerException
03-03 01:05:26.753: E/AndroidRuntime(6560):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2211)
03-03 01:05:26.753: E/AndroidRuntime(6560):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2261)
03-03 01:05:26.753: E/AndroidRuntime(6560):     at android.app.ActivityThread.access$600(ActivityThread.java:141)
03-03 01:05:26.753: E/AndroidRuntime(6560):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1256)
03-03 01:05:26.753: E/AndroidRuntime(6560):     at android.os.Handler.dispatchMessage(Handler.java:99)
03-03 01:05:26.753: E/AndroidRuntime(6560):     at android.os.Looper.loop(Looper.java:137)
03-03 01:05:26.753: E/AndroidRuntime(6560):     at android.app.ActivityThread.main(ActivityThread.java:5103)
03-03 01:05:26.753: E/AndroidRuntime(6560):     at java.lang.reflect.Method.invokeNative(Native Method)
03-03 01:05:26.753: E/AndroidRuntime(6560):     at java.lang.reflect.Method.invoke(Method.java:525)
03-03 01:05:26.753: E/AndroidRuntime(6560):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
03-03 01:05:26.753: E/AndroidRuntime(6560):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
03-03 01:05:26.753: E/AndroidRuntime(6560):     at dalvik.system.NativeStart.main(Native Method)
03-03 01:05:26.753: E/AndroidRuntime(6560): Caused by: java.lang.NullPointerException
03-03 01:05:26.753: E/AndroidRuntime(6560):     at android.database.sqlite.SQLiteOpenHelper.getDatabaseLocked(SQLiteOpenHelper.java:224)
03-03 01:05:26.753: E/AndroidRuntime(6560):     at android.database.sqlite.SQLiteOpenHelper.getWritableDatabase(SQLiteOpenHelper.java:164)
03-03 01:05:26.753: E/AndroidRuntime(6560):     at com.example.sopa.SQLiteHelper.getOrder(SQLiteHelper.java:62)
03-03 01:05:26.753: E/AndroidRuntime(6560):     at com.example.sopa.Order_disp.getInList(Order_disp.java:29)
03-03 01:05:26.753: E/AndroidRuntime(6560):     at com.example.sopa.Order_disp.onCreate(Order_disp.java:18)
03-03 01:05:26.753: E/AndroidRuntime(6560):     at android.app.Activity.performCreate(Activity.java:5133)
03-03 01:05:26.753: E/AndroidRuntime(6560):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
03-03 01:05:26.753: E/AndroidRuntime(6560):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2175)
03-03 01:05:26.753: E/AndroidRuntime(6560):     ... 11 more

感谢期待..

1 个答案:

答案 0 :(得分:0)

检查

Android Exception: Did you forget to call 'public void setup (LocalActivityManager activityGroup)'

更改此

 setContentView(R.id.th2);

 setContentView(R.layout.mylayout); // needs a layout

Second Activity的布局在哪里。没有布局设置为第二个活动

编辑:

更改

 SQLiteHelper sqh=new SQLiteHelper(context);

SQLiteHelper sqh;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_order_disp);
    sqh=new SQLiteHelper(this);