0

我试图在 2 个活动之间传递几个值,但我的代码崩溃了,我不知道为什么。从图像按钮调用代码,但应用程序只是停止。

if(v==image1){
    try {
        Calendar c = Calendar.getInstance();
        System.out.println("Current time => "+c.getTime());

        SimpleDateFormat df = new SimpleDateFormat("dd-MM-yyyy HH:mm:ss");
        String formattedDate = df.format(c.getTime());
        //now we have the time lets write it to the file
        File myFile = new File("/sdcard/.working/mx.txt");
        myFile.createNewFile();
        FileOutputStream fOut = new FileOutputStream(myFile, true);
        OutputStreamWriter myOutWriter = new OutputStreamWriter(fOut);
        myOutWriter.append(formattedDate);
        myOutWriter.append(" :");
        myOutWriter.append(tv1.getText());
        myOutWriter.append(" ,  ");
        myOutWriter.close();
        fOut.close();
    }catch (Exception e) {
        Toast.makeText(getBaseContext(), e.getMessage(), Toast.LENGTH_SHORT).show();
    }
    mx=mx++;
    Intent startabout = new Intent(RTO.this, RTOShooting.class); //this is about screen
    startabout.putExtra("mx", mx);
    startabout.putExtra("Callsign", tv1.getText());
    final int result=1;
    startActivityForResult(startabout, result);
    overridePendingTransition(R.anim.zoom_enter, R.anim.zoom_exit);
}

接收代码看起来像这样(不是我曾经得到那么远......)

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_rtoshooting);
    Bundle extras = getIntent().getExtras();
    if (extras !=null){
        int mx = extras.getInt("mx");
    }
}

有人可以引导我找到我明显想念的东西吗?

LOGCAT 输出为

02-27 11:44:46.461: W/dalvikvm(20231): threadid=1: thread exiting with uncaught exception (group=0x41053930)
02-27 11:44:46.471: E/AndroidRuntime(20231): FATAL EXCEPTION: main
02-27 11:44:46.471: E/AndroidRuntime(20231): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.nexgen.threedashone.trainer/com.nexgen.threedashone.trainer.RTOShooting}: java.lang.NullPointerException
02-27 11:44:46.471: E/AndroidRuntime(20231):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2304)
02-27 11:44:46.471: E/AndroidRuntime(20231):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2354)
02-27 11:44:46.471: E/AndroidRuntime(20231):    at android.app.ActivityThread.access$600(ActivityThread.java:150)
02-27 11:44:46.471: E/AndroidRuntime(20231):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1244)
02-27 11:44:46.471: E/AndroidRuntime(20231):    at android.os.Handler.dispatchMessage(Handler.java:99)
02-27 11:44:46.471: E/AndroidRuntime(20231):    at android.os.Looper.loop(Looper.java:137)
02-27 11:44:46.471: E/AndroidRuntime(20231):    at android.app.ActivityThread.main(ActivityThread.java:5191)
02-27 11:44:46.471: E/AndroidRuntime(20231):    at java.lang.reflect.Method.invokeNative(Native Method)
02-27 11:44:46.471: E/AndroidRuntime(20231):    at java.lang.reflect.Method.invoke(Method.java:511)
02-27 11:44:46.471: E/AndroidRuntime(20231):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:795)
02-27 11:44:46.471: E/AndroidRuntime(20231):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:562)
02-27 11:44:46.471: E/AndroidRuntime(20231):    at dalvik.system.NativeStart.main(Native Method)
02-27 11:44:46.471: E/AndroidRuntime(20231): Caused by: java.lang.NullPointerException
02-27 11:44:46.471: E/AndroidRuntime(20231):    at com.nexgen.threedashone.trainer.RTOShooting.onCreate(RTOShooting.java:40)
02-27 11:44:46.471: E/AndroidRuntime(20231):    at android.app.Activity.performCreate(Activity.java:5104)
02-27 11:44:46.471: E/AndroidRuntime(20231):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
02-27 11:44:46.471: E/AndroidRuntime(20231):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2258)
02-27 11:44:46.471: E/AndroidRuntime(20231):    ... 11 more

我也尝试过像这样创建自己的包:

        Bundle b = new Bundle();
        b.putString("Callsign", tv1.getText().toString());
        b.putInt("mx", mx);
        startabout.putExtras(b);

像这样接收:

    Bundle b = getIntent().getExtras();

    String callsign = b.getString("Callsign");

    int mx = b.getInt("mx");

但它仍然会以非常相似的 logcat 输出崩溃。tvcallsign.setText(呼号);

编辑:这是每个活动的完整代码,它并不漂亮,但我刚刚进入 java 和 android:

    package com.test.trainer;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import android.os.Bundle;
import android.app.Activity;
import android.content.ContextWrapper;
import android.content.Intent;
import android.content.res.AssetManager;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.Chronometer;
import android.widget.ImageButton;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import android.support.v4.app.NavUtils;

public class RTO extends Activity implements Chronometer.OnChronometerTickListener, OnClickListener {

     //define buttons etc
     private Button babout;
     private Button bhome;
    private ImageButton image1;
    private ImageButton image2;
    private ImageButton image3;
    private ImageButton image4;
    private ImageButton image5;
    private ImageButton image6;
    private ImageButton image7;
    private ImageButton image8; 
    private ImageButton image9; 
    private ImageButton image10;
    private ImageButton image11;
    private ImageButton image12;
    private ImageButton image13;
    private ImageButton image14;
    private ImageButton image15;
    private ImageButton image16;
    private ImageButton imagesetup;
    private ImageButton imageevent;
    private ImageButton imageend;

        TextView tv1;
        TextView tv2;
        TextView tv3;
        TextView tv4;
        TextView tv5;
        TextView tv6;
        TextView tv7;
        TextView tv8;
        TextView tv9;
        TextView tv10;
        TextView tv11;
        TextView tv12;
        TextView tv13;
        TextView tv14;
        TextView tv15;
        TextView tv16;

    public ListView lv1;
    public Chronometer mchrono;


    //define logic
    public int mx = 0;
    public int configured = 0;
    public int side = 0; //int to handle which colour buttons to use
    public int prijettype = 0; //int for primary  type
    public int secjettype = 0; //int for sec  type

    public int prijetnumber = 0; //int for primary  numbers
    public int secjetnumber = 0; //int for sec  numbers

    //hint array strings
    public String loadeddata;
    public String[] splitdata;

    public String callsign;      

    ContextWrapper mContext = null;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_rto);
        getActionBar().setDisplayHomeAsUpEnabled(true);

     //define the buttons etc for use
        babout = (Button)findViewById(R.id.babout);   //about button
        bhome = (Button)findViewById(R.id.bhome);   //home button
        image1 = (ImageButton)findViewById(R.id.image1); 
        image2 = (ImageButton)findViewById(R.id.image2);
        image3 = (ImageButton)findViewById(R.id.image3); 
        image4 = (ImageButton)findViewById(R.id.image4); 
        image5 = (ImageButton)findViewById(R.id.image5); 
        image6 = (ImageButton)findViewById(R.id.image6); 
        image7 = (ImageButton)findViewById(R.id.image7); 
        image8 = (ImageButton)findViewById(R.id.image8); 
        image9 = (ImageButton)findViewById(R.id.image9); 
        image10 = (ImageButton)findViewById(R.id.image10); 
        image11 = (ImageButton)findViewById(R.id.image11);
        image12 = (ImageButton)findViewById(R.id.image12);
        image13 = (ImageButton)findViewById(R.id.image13);
        image14 = (ImageButton)findViewById(R.id.image14); 
        image15 = (ImageButton)findViewById(R.id.image15); 
        image16 = (ImageButton)findViewById(R.id.image16); 
        imageend = (ImageButton)findViewById(R.id.imageend);  //end mission
        imagesetup = (ImageButton)findViewById(R.id.imagesetup); //setup button
        imageevent = (ImageButton)findViewById(R.id.imageevent); //event button

   tv1 = (TextView) findViewById(R.id.tv1);
        tv2 = (TextView) findViewById(R.id.tv2);
        tv3 = (TextView) findViewById(R.id.tv3);
        tv4 = (TextView) findViewById(R.id.tv4);
        tv5 = (TextView) findViewById(R.id.tv5);
        tv6 = (TextView) findViewById(R.id.tv6);
        tv7 = (TextView) findViewById(R.id.tv7);
        tv8 = (TextView) findViewById(R.id.tv8);
        tv9 = (TextView) findViewById(R.id.tv9);
        tv10 = (TextView) findViewById(R.id.tv10);
        tv11 = (TextView) findViewById(R.id.tv11);
        tv12 = (TextView) findViewById(R.id.tv12);
        tv13 = (TextView) findViewById(R.id.tv13);
        tv14 = (TextView) findViewById(R.id.tv14);
        tv15 = (TextView) findViewById(R.id.tv15);
        tv16 = (TextView) findViewById(R.id.tv16);

        image1.setOnClickListener(this);
        image2.setOnClickListener(this);
        image3.setOnClickListener(this);
        image4.setOnClickListener(this);  
        image5.setOnClickListener(this);
        image6.setOnClickListener(this);   
        image7.setOnClickListener(this);
        image8.setOnClickListener(this);
        image9.setOnClickListener(this);
        image10.setOnClickListener(this);
        image11.setOnClickListener(this);
        image12.setOnClickListener(this);
        image13.setOnClickListener(this);  
        image14.setOnClickListener(this);   
        image15.setOnClickListener(this);
        image16.setOnClickListener(this); 
        imageend.setOnClickListener(this);
        imagesetup.setOnClickListener(this);
        imageevent.setOnClickListener(this);   

        settextviews();
            setimageviews();
            hidethem();
            arrayload();

 }//end oncreate

public void arrayload() {
}

public void settextviews() {

                    //load the asset files themselves
                    StringBuilder text = new StringBuilder();
                    try {
                         BufferedReader br = new BufferedReader(new FileReader("/sdcard/NexGen/RTOLOGS/.working/1.txt"));
                            String line;

                            while ((line = br.readLine()) != null) {
                                text.append(line);
                                text.append('\n');
                            }
                                //set the text we recovered to the TextView
                                tv1.setText(text);

                //load progress info
                StringBuilder text2 = new StringBuilder();
                     BufferedReader br2 = new BufferedReader(new FileReader("/sdcard/NexGen/RTOLOGS/.working/2.txt"));
                            String line2;
                                while ((line2 = br2.readLine()) != null) {
                                text2.append(line2);
                                text2.append('\n');
                            }
                            tv2.setText(new String(text2));
} //end try
                    catch (IOException ex) {
                        return;
                    }

public void setimageviews() {
//nothing here yet

}

    public void hidethem() {
        //nothing here yet

}

  public void onClick(View v) {
        // TODO Auto-generated method stub
        if (v==bhome) {
            // TODO Auto-generated method stub
            Intent starthome = new Intent(RTO.this, Main.class); //this is about screen
        startActivity(starthome);
        overridePendingTransition(R.anim.zoom_enter, R.anim.zoom_exit);
        }

        if (v==babout) {
            Intent startabout = new Intent(RTO.this, AboutScreen.class); //this is about screen
            startActivity(startabout);
            overridePendingTransition(R.anim.zoom_enter, R.anim.zoom_exit);
            }
        if(v==image1){
            mx=mx++;
            try {
                Calendar c = Calendar.getInstance();
                System.out.println("Current time => "+c.getTime());

                SimpleDateFormat df = new SimpleDateFormat("HH:mm:ss");
                String formattedDate = df.format(c.getTime());
                //now we have the time lets write it to the file
                String fileName = String.valueOf(mx) +".txt";   
                File myFile = new File("/sdcard/test/LOGS/.working/mx/" +fileName);
                myFile.createNewFile();
                FileOutputStream fOut = new FileOutputStream(myFile, true);
                OutputStreamWriter myOutWriter = 
                                        new OutputStreamWriter(fOut);
                myOutWriter.append(formattedDate);
                myOutWriter.append(" :");
                myOutWriter.append(tv1.getText());
                myOutWriter.append(" ,  ");
                myOutWriter.close();
                fOut.close();
            }
            catch (Exception e) {
                Toast.makeText(getBaseContext(), e.getMessage(),
                        Toast.LENGTH_SHORT).show();
            }


            Bundle b = new Bundle();
            String callsign = (String)tv1.getText().toString();
            b.putString("Callsign", callsign);
            b.putInt("mx", mx);
            Intent startabout = new Intent(RTO.this, RTOShooting.class); //this is next screen
            startabout.putExtras(b);
            startActivity(startabout);
            overridePendingTransition(R.anim.zoom_enter, R.anim.zoom_exit);
            }   


 } //end onClick

//nothing below here yet
  @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_rto, menu);
        return true;
    }


    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case android.R.id.home:
                NavUtils.navigateUpFromSameTask(this);
                return true;
        }
        return super.onOptionsItemSelected(item);
    }

    public void onChronometerTick(Chronometer chronometer) {
        // TODO Auto-generated method stub

    }

}

接收活动如下所示:

package com.test.trainer;

import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStreamWriter;

import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageButton;
import android.widget.TextView;
import android.widget.Toast;

public class RTOShooting extends Activity implements OnClickListener {

    private ImageButton image1;
    private ImageButton image2;
    private ImageButton image3;
    private ImageButton image3close;
    private ImageButton image3medium;
    private ImageButton image3short;

    public int MX;

TextView tvcallsign;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_rtoshooting);
        Bundle b = getIntent().getExtras();
         String callsign = b.getString("Callsign");
          int mx = b.getInt("mx");
        MX = mx;
        tvcallsign.setText(callsign);


 tvcallsign = (TextView) findViewById(R.id.tvcallsign);

 } //end on create

 public void onClick(View v) {
        // TODO Auto-generated method stub
        if (v==image1) {

        }
}

 @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_rtoshooting, menu);
        return true;
    }
4

1 回答 1

0

假设您的代码和 StackTrace 对齐,您会在跟踪中看到以下行:

在 com.nexgen.threedashone.trainer.RTOShooting.onCreate(RTOShooting.java:40)

只需计算 RTOShooting 中的行数,我就到这里了:

tvcallsign.setText(callsign);
tvcallsign = (TextView) findViewById(R.id.tvcallsign);

您正在尝试在下一行中设置您要查找的对象的文本。交换它们,一切都应该顺利。

于 2013-03-02T12:17:50.480 回答