1

我正在开发的 android 应用程序上有一些按钮,突然一些按钮单击不起作用。这是我的按钮单击的代码。

cancel.setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View v) {      
                    myIntent=new Intent(relationship.this, relationship.class);

                    String uname;
                    uname=myValues.getString("value").toString();

                    myIntent.putExtra("value", uname);
                    startActivity(myIntent);

                }

            }); 

而且我也尝试了不同的方法更改为view.onclicklistener 但是我得到的以下错误是没有用的。

09-24 08:17:03.403: E/AndroidRuntime(1851): FATAL EXCEPTION: main
09-24 08:17:03.403: E/AndroidRuntime(1851): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.newairways/com.example.newairways.relationship}: java.lang.NullPointerException
09-24 08:17:03.403: E/AndroidRuntime(1851):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
09-24 08:17:03.403: E/AndroidRuntime(1851):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
09-24 08:17:03.403: E/AndroidRuntime(1851):     at android.app.ActivityThread.access$600(ActivityThread.java:141)
09-24 08:17:03.403: E/AndroidRuntime(1851):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
09-24 08:17:03.403: E/AndroidRuntime(1851):     at android.os.Handler.dispatchMessage(Handler.java:99)
09-24 08:17:03.403: E/AndroidRuntime(1851):     at android.os.Looper.loop(Looper.java:137)
09-24 08:17:03.403: E/AndroidRuntime(1851):     at android.app.ActivityThread.main(ActivityThread.java:5041)
09-24 08:17:03.403: E/AndroidRuntime(1851):     at java.lang.reflect.Method.invokeNative(Native Method)
09-24 08:17:03.403: E/AndroidRuntime(1851):     at java.lang.reflect.Method.invoke(Method.java:511)
09-24 08:17:03.403: E/AndroidRuntime(1851):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
09-24 08:17:03.403: E/AndroidRuntime(1851):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
09-24 08:17:03.403: E/AndroidRuntime(1851):     at dalvik.system.NativeStart.main(Native Method)
09-24 08:17:03.403: E/AndroidRuntime(1851): Caused by: java.lang.NullPointerException
09-24 08:17:03.403: E/AndroidRuntime(1851):     at com.example.newairways.relationship.onCreate(relationship.java:81)
09-24 08:17:03.403: E/AndroidRuntime(1851):     at android.app.Activity.performCreate(Activity.java:5104)
09-24 08:17:03.403: E/AndroidRuntime(1851):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
09-24 08:17:03.403: E/AndroidRuntime(1851):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
09-24 08:17:03.403: E/AndroidRuntime(1851):     ... 11 more

这是我的代码。

package com.example.xxx;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.SeekBar;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.SeekBar.OnSeekBarChangeListener;



public class relationship extends Activity implements OnSeekBarChangeListener {

    //Variables Assign
    private SeekBar mSeekBar1, mSeekBar2;
    private TextView tv1, tv2;
    private Button ok,nextQ,cancel;
    private ProgressDialog dialog = null;

    Intent myIntent;
    Bundle myValues;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.relationships);
        myValues=getIntent().getExtras();
        mSeekBar1 = (SeekBar) findViewById(R.id.Q1a); 
        mSeekBar1.setOnSeekBarChangeListener(this);
        mSeekBar2= (SeekBar) findViewById(R.id.Q1b); 
        mSeekBar2.setOnSeekBarChangeListener(this);


        tv1 = (TextView)findViewById(R.id.Ans1a);
        tv2 = (TextView)findViewById(R.id.Ans1b);

        //Userame Assign
       TextView welcomeMsg = (TextView)findViewById(R.id.username);
        welcomeMsg.setText("name : "+myValues.getString("value"));

           ok = (Button)findViewById(R.id.ok);
            nextQ = (Button)findViewById(R.id.nextquestion);
            cancel= (Button)findViewById(R.id.cancel);

            ok.setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View arg0) {

                    //Storing in Global variables
                    String a = tv1.getText().toString();
                    String Q1b = tv2.getText().toString();
                    GlobalVars.setSeekBarValue(a);
                    GlobalVars.setSeekBarValue1b(Q1b);
                    Toast.makeText(relationship.this, "Datastored",Toast.LENGTH_LONG ).show();
                }

           });     


            nextQ.setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View v) {      
                    myIntent=new Intent(relationship.this, start.class);

                    String uname;
                    uname=myValues.getString("value").toString();

                    myIntent.putExtra("value", uname);
                    startActivity(myIntent);

                }

            }); 

            cancel.setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View v) {      
                    myIntent=new Intent(relationship.this, relationship.class);

                    String uname;
                    uname=myValues.getString("value").toString();

                    myIntent.putExtra("value", uname);
                    startActivity(myIntent);

                }

            }); 




    }

    @Override
    public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {

        switch (seekBar.getId())
        {

        case R.id.Q1a:

            tv1.setText(Integer.toString(progress)+"%") ;
            //Toast.makeText(relationship.this, "Seekbar Value : " + progress, Toast.LENGTH_SHORT).show();
            break;

case R.id.Q1b:
    tv2.setText(Integer.toString(progress)+"%") ;
            //Toast.makeText(relationship.this, "Seekbar Value : " + progress, Toast.LENGTH_SHORT).show();
            break;

        } 
    }






    @Override
    public void onStartTrackingTouch(SeekBar arg0) {
        // TODO Auto-generated method stub

    }

    @Override
    public void onStopTrackingTouch(SeekBar arg0) {
        // TODO Auto-generated method stub

    }

}
4

4 回答 4

1

你的关系活动中显然有 NPE。我敢打赌 myValues.getString("value") 返回 null,然后作为“value”额外传递。

于 2013-09-24T08:29:15.533 回答
1

NullPointerException位于:

Relationship.java- 81 号线

在课堂上检查这一行,你在分配时得到空值。

于 2013-09-24T08:34:22.630 回答
0

如果您使用的是局部变量,那么首先初始化那个

String uname=null;
 uname=myValues.getString("value").toString();

其次,您正在启动相同的活动课程,这毫无意义。第三件事是在你得到它时检查 uname 值是否null

于 2013-09-24T08:29:37.367 回答
0

与突出显示的第 81 行共享您的 relationship.java 文件代码。我希望您没有忘记在清单文件中提及此活动。

于 2013-09-24T09:26:53.540 回答