mainListView.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
String item = ((TextView)view).getText().toString();
Toast.makeText(getBaseContext(), item, Toast.LENGTH_LONG).show();
tcpSocket.SendMessage(FramePacket.FramesendMessageCmd(item));
}
});
herer是我的代码。我想在点击消息项后开始另一个活动。 我尝试了很多。但不行。任何人都知道请帮助我。怎么解决这个问题。我尝试了很多。它给出了nullpointer异常错误。
catlog错误
09-02 13:36:44.364: E/AndroidRuntime(28601): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.dispatchsystem/com.dispatchsystem.JobStartedScreen}: java.lang.NullPointerException
09-02 13:36:44.364: E/AndroidRuntime(28601): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1967)
09-02 13:36:44.364: E/AndroidRuntime(28601): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1992)
09-02 13:36:44.364: E/AndroidRuntime(28601): at android.app.ActivityThread.access$600(ActivityThread.java:127)
09-02 13:36:44.364: E/AndroidRuntime(28601): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1158)
09-02 13:36:44.364: E/AndroidRuntime(28601): at android.os.Handler.dispatchMessage(Handler.java:99)
09-02 13:36:44.364: E/AndroidRuntime(28601): at android.os.Looper.loop(Looper.java:137)
09-02 13:36:44.364: E/AndroidRuntime(28601): at android.app.ActivityThread.main(ActivityThread.java:4448)
09-02 13:36:44.364: E/AndroidRuntime(28601): at java.lang.reflect.Method.invokeNative(Native Method)
09-02 13:36:44.364: E/AndroidRuntime(28601): at java.lang.reflect.Method.invoke(Method.java:511)
09-02 13:36:44.364: E/AndroidRuntime(28601): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:823)
09-02 13:36:44.364: E/AndroidRuntime(28601): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:590)
09-02 13:36:44.364: E/AndroidRuntime(28601): at dalvik.system.NativeStart.main(Native Method)
09-02 13:36:44.364: E/AndroidRuntime(28601): Caused by: java.lang.NullPointerException
09-02 13:36:44.364: E/AndroidRuntime(28601): at com.dispatchsystem.JobStartedScreen.onCreate(JobStartedScreen.java:65)
09-02 13:36:44.364: E/AndroidRuntime(28601): at android.app.Activity.performCreate(Activity.java:4465)
09-02 13:36:44.364: E/AndroidRuntime(28601): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
09-02 13:36:44.364: E/AndroidRuntime(28601): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1931)
jobstartedscreen
public class JobStartedScreen extends Activity implements OnMessageReceived{
TextView tv_JobNo;
static TextView tv_JobKm;
static TextView tv_JobwaitngTime;
TextView tvjobstartedtime;
Button btn_jobFinsh;
Button btn_msg;
boolean _ThreadFlag=false;
boolean _ResumeThread=false;
private Window w;
Thread t=null;
String tag="Jobstartscreen";
private static MyThread sThread;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.jobstartedscreen);
if (savedInstanceState==null){
Contsants.VehicleStartOdometer=0.0F;
sThread=new MyThread(mHandler);
sThread.start();
}
StateManager.DeviceStatus=StateManager.hired;
tcpSocket.setmMessageListener(this);
tv_JobNo =(TextView)findViewById(R.id.tvJobid);
Bundle bundle = getIntent().getExtras();
tv_JobNo.setText(bundle.get("Jobno").toString());
tv_JobKm =(TextView)findViewById(R.id.tvjobKm);
tv_JobwaitngTime =(TextView)findViewById(R.id.tvjobWaittime);
tv_JobwaitngTime.setText("Waiting Time : 0:0");
//update the Km every 1 sec
Log.d(tag, "On created "+_ResumeThread);
tvjobstartedtime=(TextView)findViewById(R.id.tv_jobStarttime);
tvjobstartedtime.setText("Start Time :"+Contsants.jobStartTime);
btn_jobFinsh= (Button)findViewById(R.id.btn_jobFinsh);
btn_jobFinsh.setOnClickListener(onClickListener);
btn_msg= (Button)findViewById(R.id.btn_msg);
btn_msg.setOnClickListener(onClickListener);
// TODO Auto-generated method stub
}
@Override
public void onStart()
{
super.onStart();
}
@Override
public void onResume()
{
super.onResume();
w = this.getWindow();
w.addFlags(LayoutParams.FLAG_DISMISS_KEYGUARD);
w.addFlags(LayoutParams.FLAG_SHOW_WHEN_LOCKED);
w.addFlags(LayoutParams.FLAG_TURN_SCREEN_ON);
Log.d("job start on resume",_ThreadFlag+" "+_ResumeThread);
}
@Override
protected void onRestoreInstanceState(Bundle savedInstanceState){
super.onRestoreInstanceState(savedInstanceState);
if (sThread.isAlive()){
sThread.setHandler(mHandler);
}
}
@Override
protected void onDestroy(){
super.onDestroy();
if (sThread.isAlive()){
sThread.setHandler(null);
}
}
boolean _Paused=false;
@Override
public void onPause()
{
super.onPause();
_Paused=true;
}
@Override
public void onBackPressed() {
// do nothing.
}
private OnClickListener onClickListener = new OnClickListener() {
@Override
public void onClick(final View v) {
switch(v.getId()){
case R.id.btn_jobFinsh:
//DO something
_ThreadFlag=true;
StateManager.JobStatus=StateManager.jobstatus_dropped;
tcpSocket.SendMessage(FramePacket.FrameJobStatusCmd(Contsants.Jobno,StateManager.JobStatus) );
SimpleDateFormat sdf = new SimpleDateFormat("dd-MM-yy HH:mm");
String currentDateandTime = sdf.format(new Date());
Contsants.jobEndTime=currentDateandTime;
Intent i = new Intent(getApplicationContext(),JobCompletionScreen.class);
startActivity(i);
sThread.setHandler(null);
finish();
break;
case R.id.btn_msg:
/*Intent iNAV = new Intent(Intent.ACTION_VIEW,
Uri.parse("http://maps.google.com/maps?saddr=12.920629,77.610643&daddr=12.990346, 77.589521")
);
startActivity(iNAV);*/
Intent imsg = new Intent(getApplicationContext(),MessageList.class);
startActivity(imsg);
break;
}
}
};
static Handler mHandler =new Handler(){
@Override
public void handleMessage(Message message){
//update UI
Bundle b = message.getData();
float odometer = b.getFloat("My Odometer");
int waiting =b.getInt("waiting Time");
tv_JobKm.setText(odometer+" Km");
tv_JobwaitngTime.setText("Waiting Time : "+Integer.toString((waiting / 60)) +":"+ Integer.toString((waiting% 60)));
}
};
private class MyThread extends Thread{
private Handler mHandler;
public MyThread(Handler handler){
super();
mHandler=handler;
}
@Override
public void run(){
//some long operation
if (mHandler!=null)
mHandler.sendEmptyMessage(0);
_ResumeThread=true;
while(!_ThreadFlag)
{
try {
Thread.sleep(1000);
Message msg = new Message();
Bundle b = new Bundle();
b.putFloat("My Odometer", (float) (gpsdataElements.Distance-Contsants.jobStartKm));
if(gpsdataElements.Speed==0.0)
{
Contsants.cont_WaitingTimeInSec++;
}
b.putInt("waiting Time", Contsants.cont_WaitingTimeInSec);
// tv_JobwaitngTime.setText("Waiting Time : "+Integer.toString((Contsants.cont_WaitingTimeInSec / 60)) +":"+ Integer.toString((Contsants.cont_WaitingTimeInSec % 60)));
//send message to the handler with the current message handler
msg.setData(b);
// send message to the handler with the current message handler
mHandler.sendMessage(msg);
} catch (Exception e) {
Log.v("Error", e.toString());
}
}
}
public void setHandler(Handler handler){
mHandler=handler;
}
}
@Override
public void messageReceived(String message) {
// TODO Auto-generated method stub
//Toast.makeText(getApplicationContext(), "Job StartScreem : Recived data :"+message, Toast.LENGTH_LONG).show();
}
}
答案 0 :(得分:2)
您可以在侧面点击
使用处理程序mainListView.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
String item = ((TextView)view).getText().toString();
Toast.makeText(getBaseContext(), item, Toast.LENGTH_LONG).show();
tcpSocket.SendMessage(FramePacket.FramesendMessageCmd(item));
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
MyAct.this.startActivity(new Intent(MyAct.this,Abc.class));
}
}, 5000);
}
});
public void run() { 在此插入您的下一个活动
答案 1 :(得分:0)
展示你的吐司之后呢。
Intent intent= new Intent(youractivitycontext, AnotherActivity.class);
startActivity(intent);
答案 2 :(得分:0)
将您的代码编辑为:
mainListView.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
String item = ((TextView)view).getText().toString();
Toast.makeText(getBaseContext(), item, Toast.LENGTH_LONG).show();
Intent intent= new Intent(youractivitycontext, AnotherActivity.class);
startActivity(intent);
}
});
这将带您进入由 AnotherActivity.class 指定的其他活动。您也可以将数据传递给下一个活动,因为您似乎正在尝试这样做。并且有几种方法可以做到这一点。去HERE了解意图