在类之间设置值(通过set / get方法)

时间:2014-08-01 11:19:59

标签: java android

我有一个用于定义变量的模型类,当我尝试将一些值从MainActivity设置为Model类时,我遇到了错误。

来自我的MainActivity.class

public class MainActivity extends Activity {
   private ListView productList;
   private OfferModel offerModel;

        @Override
    protected void onCreate (Bundle savedInstanceState){

        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS);
        setContentView(R.layout.activity_main);
        productList = (ListView)findViewById(R.id.listView1);

        ...many codes here...

        productList.setOnItemClickListener(new OnItemClickListener() {

            @SuppressWarnings("deprecation")
            @Override
            public void onItemClick(AdapterView<?> arg0, View arg1, int position,
                    long arg3) {

                    offerModel.setProductName(arg0.getItemAtPosition(position).toString());


            }
        });

    }}

这来自我的Model类

public class OfferModel {


    private String productName;
    public String getProductName(){return this.productName;}
    public void setProductName(String pName){ this.productName=pName;}
}

这是我的日志结果

08-01 11:17:46.446: W/dalvikvm(2305): threadid=1: thread exiting with uncaught exception (group=0xa4b70648)
08-01 11:17:46.446: E/AndroidRuntime(2305): FATAL EXCEPTION: main
08-01 11:17:46.446: E/AndroidRuntime(2305): java.lang.NullPointerException
08-01 11:17:46.446: E/AndroidRuntime(2305):     at stok.kontrol.noyagrup.MainActivity$2.onItemClick(MainActivity.java:71)
08-01 11:17:46.446: E/AndroidRuntime(2305):     at android.widget.AdapterView.performItemClick(AdapterView.java:298)
08-01 11:17:46.446: E/AndroidRuntime(2305):     at android.widget.AbsListView.performItemClick(AbsListView.java:1100)
08-01 11:17:46.446: E/AndroidRuntime(2305):     at android.widget.AbsListView$PerformClick.run(AbsListView.java:2788)
08-01 11:17:46.446: E/AndroidRuntime(2305):     at android.widget.AbsListView$1.run(AbsListView.java:3463)
08-01 11:17:46.446: E/AndroidRuntime(2305):     at android.os.Handler.handleCallback(Handler.java:730)
08-01 11:17:46.446: E/AndroidRuntime(2305):     at android.os.Handler.dispatchMessage(Handler.java:92)
08-01 11:17:46.446: E/AndroidRuntime(2305):     at android.os.Looper.loop(Looper.java:137)
08-01 11:17:46.446: E/AndroidRuntime(2305):     at android.app.ActivityThread.main(ActivityThread.java:5103)
08-01 11:17:46.446: E/AndroidRuntime(2305):     at java.lang.reflect.Method.invokeNative(Native Method)
08-01 11:17:46.446: E/AndroidRuntime(2305):     at java.lang.reflect.Method.invoke(Method.java:525)
08-01 11:17:46.446: E/AndroidRuntime(2305):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
08-01 11:17:46.446: E/AndroidRuntime(2305):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
08-01 11:17:46.446: E/AndroidRuntime(2305):     at dalvik.system.NativeStart.main(Native Method)

我失踪了什么?

提前致谢。

3 个答案:

答案 0 :(得分:6)

OfferModel offerModel未实例化,且为null。因此,当您致电offerModel.setProductName(arg0.getItemAtPosition(position).toString());时,您会收到例外情况。

答案 1 :(得分:2)

您尚未在代码中初始化private OfferModel offerModel;并且您正在使用offerModel.setProductName(arg0.getItemAtPosition(position).toString());,因为它会抛出NPE

首先初始化它productName null尚未初始化,但你可以使用parameterized constructor 初始化它(你必须首先添加构造函数)

offerModel=new OfferModel(productNameValue);//Pass initial value of `productName`
//So it will first initialize your productName 

添加构造函数

public OfferModel(String pro){
   productName=pro;
}

或者从代码中我可以看到您喜欢this个关键字,因此您可以像 这样

public OfferModel(String productName){
   this.productName=productName;
}

答案 2 :(得分:1)

         productList.setOnItemClickListener(new OnItemClickListener() {

        @SuppressWarnings("deprecation")
        @Override
        public void onItemClick(AdapterView<?> arg0, View arg1, int position,
                long arg3) {
                 offerModel= new OfferModel();
                 offerModel.setProductName(arg0.getItemAtPosition(position).toString());


        }
    });