私が開発している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
}
}