刷卡刷新时出错

时间:2015-06-09 13:45:20

标签: java android

我有两项活动 -
1. MainActivity
2. Display_Activity

主要活动有一个内部类“检查”。 我试图通过在Display_Activity中创建其对象来访问内部类“check”中编写的代码 根据我的说法,我可以在内部类中运行代码,但是共享偏好从某个地方变为空
当我滑动刷新时,应用程序产生一个空对象异常。我无法得到我提供null的地方。我试图调试但找不到它。如果可以的话请帮忙。

我提供了活动和logcat。

Main_Activity.java

public class MainActivity extends Activity {
String value;
String orientation="false";
EditText edtUrl;
String tknnumber;

SharedPreferences sharedpreferences;
String URL="http://192.168.1.101:8080/DoctorApp/newjsp2.jsp?token=";

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    edtUrl = (EditText) findViewById(R.id.edtURL);
    orientation="true";
}

public void CallURL(View view) {
    check check1 = new check();
    tknnumber = edtUrl.getText().toString();
    boolean result = isNetworkAvailable();
    if (!result) {
        Toast.makeText(getApplicationContext(), "No Internet Connection", Toast.LENGTH_LONG).show();
    } else {
        check1.Checking_network(tknnumber);
    }
}
public class check{
public void Checking_network(String tkn){
        URL = URL.concat(tkn);
     (new ParseURL()).execute(new String[]{URL});

    }

private class ParseURL extends AsyncTask<String, Void, String> {

    @Override
    protected String doInBackground(String... strings) {
        StringBuffer buffer = new StringBuffer();

        try {
            //Log.d("JSwa", "Connecting to [" + strings[0] + "]");
            Document doc = Jsoup.connect(strings[0]).get();
            String token = doc.getElementById("token").ownText();
            String p_name = doc.getElementById("pname").ownText();
            String gender = doc.getElementById("gender").ownText();
            String city = doc.getElementById("city").ownText();
            String date = doc.getElementById("date").ownText();
            String doctor=doc.getElementById("doctor").ownText();
            String time_left=doc.getElementById("time").ownText();

            buffer.append(token+"\n"+p_name+"\n"+gender+"\n"+city+"\n"+date+"\n"+doctor+"\n"+time_left);


        } catch (Exception t) {

            buffer.delete(0,buffer.length());
            buffer.append("Error");
            t.printStackTrace();
        }

        return buffer.toString();

    }

    @Override
    protected void onPostExecute(String s) {
        super.onPostExecute(s);
        if(s.equalsIgnoreCase("Error")){
            Toast.makeText(MainActivity.this, "Sorry! Something went wrong. Make sure you have entered the correct token number", Toast.LENGTH_LONG).show();
        }
        else{
            try{
        callIntent(s);
    }
        catch(Exception e){
            refreshCall(s);}
    }}}
}
public boolean isNetworkAvailable(){

    ConnectivityManager cm=(ConnectivityManager)getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo networkInfo=cm.getActiveNetworkInfo();
    if(networkInfo!=null && networkInfo.isConnected()){
        return true;
    }
    return false;
}
Context context=this;
void callIntent(String s){
    Intent intent;
    intent = new Intent(context, display_activity.class);
    intent.putExtra("info",s);
    intent.putExtra("token_number",tknnumber);
    startActivity(intent);}

void refreshCall(String s){

    SharedPreferences pref=getSharedPreferences("mydata",Context.MODE_PRIVATE);
    SharedPreferences.Editor editor = pref.edit();
    editor.putString("key", s);
    editor.commit();
}}

Display_activity

public class display_activity extends Activity {
TextView token_name,patient,gender,location,date,doctor,token_null,time_left;
String token_received;
SwipeRefreshLayout mSwipeRefreshLayout;
MainActivity mainActivity=new MainActivity();
MainActivity.check abc=mainActivity.new check();
String URL="http://192.168.1.101:8080/DoctorApp/newjsp2.jsp?token=";
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_display_activity);
    mSwipeRefreshLayout = (SwipeRefreshLayout) findViewById(R.id.activity_swipe_refresh_layout);
    token_name=(TextView) findViewById(R.id.tkn);
    patient=(TextView) findViewById(R.id.patient);
    gender = (TextView) findViewById(R.id.gender);
    location=(TextView) findViewById(R.id.location);
    date=(TextView) findViewById(R.id.date1);
    doctor=(TextView) findViewById(R.id.doctor1);
    token_null=(TextView)findViewById(R.id.token);
    time_left=(TextView)findViewById(R.id.time_left);

    Intent intent=getIntent();
    String infor=intent.getStringExtra("info");
    token_received=intent.getStringExtra("token_number");
        breaking(infor);
   mSwipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
        @Override
        public void onRefresh() {
                refreshContent();
            }});}

public void breaking(String a){
    String delimiter = "\n";
    String[] temp;
    temp = a.split(delimiter);
    try{
        token_name.setText(temp[0]);
        patient.setText(temp[1]);
        gender.setText(temp[2]);
        location.setText(temp[3]);
        date.setText(temp[4]);
        doctor.setText(temp[5]);
        time_left.setText(temp[6]+" mins (approx)");
    }
    catch (ArrayIndexOutOfBoundsException e){

        token_name.setText(a);
        e.printStackTrace();
    }
}

public void refreshContent(){
    boolean result=isNetworkAvailable();
    if(!result){
        Toast.makeText(getApplicationContext(), "No Internet Connection", Toast.LENGTH_LONG).show();
    }
    else {

        abc.Checking_network(token_received);
        SharedPreferences pref = getSharedPreferences("mydata", Context.MODE_PRIVATE);
        String information=pref.getString("key","NA");
        breaking(information);
        mSwipeRefreshLayout.setRefreshing(false);
}}


public void Callprevious(View view){
    Intent intent;
    intent = new Intent(this , MainActivity.class);
    startActivity(intent);
}
}

logcat刷卡刷新

  06-10 20:00:24.399  12012-12012/com.example.nmn.ajsouptry E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.example.nmn.ajsouptry, PID: 12012
java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.SharedPreferences android.content.Context.getSharedPreferences(java.lang.String, int)' on a null object reference
        at android.content.ContextWrapper.getSharedPreferences(ContextWrapper.java:169)
        at com.example.nmn.ajsouptry.MainActivity.refreshCall(MainActivity.java:144)
        at com.example.nmn.ajsouptry.MainActivity$check$ParseURL.onPostExecute(MainActivity.java:115)
        at com.example.nmn.ajsouptry.MainActivity$check$ParseURL.onPostExecute(MainActivity.java:56)

我是这个网站的新手,请给我时间进行改进。请注意我的错误,如果它看起来很奇怪,你可以简单地忽略它。谢谢。

1 个答案:

答案 0 :(得分:1)

这里有很多问题,而且大部分都与Context有关。

This answer讨论Contexta linked post in the comments也解释得很清楚。

此处使用的Context种类型不同,每个Activity Context只能使用ActivityName.this(即thisContext或您想要的任何其他方式关于活动的AsyncTask

潜在的问题是您尝试从另一个Activity引用Activity内部类new MainActivity()。您正试图将ContextContext context = this;实例化,您应该从不这样做。

由于多个活动需要执行任务,因此只需将其取出并使其成为自己的类。您可以创建一个构造函数来传递url变量或所需的任何参数,包括活动onCreate()

此外,您在null运行之前使用Context(您在方法之外使用它)。这将导致onCreate(),因为<AnchorPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" styleClass="header-bar" stylesheets="@standards.css" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1"> .header-bar { -fx-background-color: blue; } 之前不可用