0

我正在尝试处理我的 android 项目,但在我的主函数中出现空指针异常。因此,基于输入的 id,我希望设置从 editText 输入的 id 并在我调用 get 函数时检索它,这将完成条件的 where 子句。这是我的代码:

/*---GetSet.java ---- */

public class GetSet {

long gwid=0;

public void setId(long id)
{
    gwid = id;
}

public long getId()
{
    return gwid;
}

}

以下是我的 MainActivity.java 文件

public class MainActivity extends Activity {

EditText etGWid;
Button OKbtn;

public final static String EXTRA_MESSAGE = "com.example.myfirstapp.MESSAGE";

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    
    
    etGWid = (EditText)findViewById(R.id.etGWID);
    OKbtn = (Button)findViewById(R.id.OKbtn);
    
    final GetSet obj_getset = null;
    final SharedPreferences app_preferences = PreferenceManager.getDefaultSharedPreferences(this);  
            // Create a shared preferences variable using the SharedPreferenceManager for storing GWids
    
    SharedPreferences.Editor editor = app_preferences.edit();  // We also need a shared preferences editor to handle the shared preference requests
    // Call the edit method (library function) to editor variable can edit the key, values.
    
    editor.putInt("47688507", 47688507);   // put in the shared preferences values of user GWids and give them a key for retrieval purposes
    editor.putInt("1234567", 1234567);
    editor.putInt("7654321", 7654321);
    
    editor.commit();   //commit is necessary to save the shared preferences
    
    
    
    OKbtn.setOnClickListener(new View.OnClickListener() {
        
        @SuppressWarnings("null")
        @Override
        public void onClick(View arg0) {
            // TODO Auto-generated method stub
            
            String gwidCheck = etGWid.getText().toString();  //get the value user enters for GWid
            
            if(app_preferences.contains(gwidCheck))       // this will check the stored shared preferences and compare with the value entered
            {
                Context context = getApplicationContext();
                CharSequence text = "Login Successfull";
                int duration = Toast.LENGTH_SHORT;                              //If it exists, then create a toast message for success

                
                
                //etGWid.setText("");    // make the textbox empty
                long setid = Long.parseLong(gwidCheck);   // take the string gwid and convert to long
                obj_getset.setId(setid);    // set the gwid entered
                Toast toast = Toast.makeText(context, text, duration);
                toast.show();
                intentfunction();
            } 
            else
            {
                Context context = getApplicationContext();
                CharSequence text = "Login Failed";                     // If doesnt exist, create a toast for fail
                int duration = Toast.LENGTH_SHORT;

                Toast toast = Toast.makeText(context, text, duration);
                toast.show();
            }
            
        }
    });
}


private void intentfunction()
{
     Intent intent = new Intent(this, SelectOptions.class);
     //editText = (EditText) findViewById(R.id.editText1);
     //editText = new EditText(this);
     
    String message = "TestHello";
    intent.putExtra(EXTRA_MESSAGE, message);
    startActivity(intent);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.activity_main, menu);
    return true;
}

}

第 69 行是我得到以下问题的地方:

 obj_getset.setId(setid);

这是 MySQLitehelper.java 文件中的一种方法,用于根据 id 检索行:

 public Cursor getRecord(long rowId) throws SQLException
{
        Cursor mCursor =
        db.query(true, TABLE_NAME, new String[] {COLUMN_ID,
        COLUMN_DATE, COLUMN_LOCATION, COLUMN_TIME},
        COLUMN_ID + "=" + getset.getId(), null, null, null, null, null);
        if (mCursor != null) 
        {
            mCursor.moveToFirst();
        }
 return mCursor;
 }

我的问题是,如何设置用户输入的值以便我可以获取该值并基于该值,我可以将条件放在我的 where 子句中,就像上面一样。我哪里错了?

日志猫

11-29 21:19:22.525: D/AndroidRuntime(10323): Shutting down VM
11-29 21:19:22.525: W/dalvikvm(10323): threadid=1: thread exiting with uncaught         exception (group=0x40a70930)
11-29 21:19:22.583: E/AndroidRuntime(10323): FATAL EXCEPTION: main
11-29 21:19:22.583: E/AndroidRuntime(10323): java.lang.NullPointerException
11-29 21:19:22.583: E/AndroidRuntime(10323):    at     com.example.upd.MainActivity$1.onClick(MainActivity.java:69)
11-29 21:19:22.583: E/AndroidRuntime(10323):    at     android.view.View.performClick(View.java:4202)
11-29 21:19:22.583: E/AndroidRuntime(10323):    at android.view.View$PerformClick.run(View.java:17340)
11-29 21:19:22.583: E/AndroidRuntime(10323):    at android.os.Handler.handleCallback(Handler.java:725)
11-29 21:19:22.583: E/AndroidRuntime(10323):    at android.os.Handler.dispatchMessage(Handler.java:92)
11-29 21:19:22.583: E/AndroidRuntime(10323):    at android.os.Looper.loop(Looper.java:137)
11-29 21:19:22.583: E/AndroidRuntime(10323):    at android.app.ActivityThread.main(ActivityThread.java:5039)
11-29 21:19:22.583: E/AndroidRuntime(10323):    at java.lang.reflect.Method.invokeNative(Native Method)
11-29 21:19:22.583: E/AndroidRuntime(10323):    at java.lang.reflect.Method.invoke(Method.java:511)
11-29 21:19:22.583: E/AndroidRuntime(10323):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
11-29 21:19:22.583: E/AndroidRuntime(10323):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
11-29 21:19:22.583: E/AndroidRuntime(10323):    at dalvik.system.NativeStart.main(Native Method)
11-29 21:19:25.702: I/Process(10323): Sending signal. PID: 10323 SIG: 9

程序意外关闭。

4

1 回答 1

2

final GetSet obj_getset = null;

好吧,你当然会得到一个 nullPointerException;您的 obj_getset 在那里被定义为 null 。

于 2012-11-29T21:53:10.143 回答