在刷卡刷新错误 [英] Error on Swipe Refresh

查看:375
本文介绍了在刷卡刷新错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have two activities-
1. MainActivity
2. Display_Activity

主要活动有一个内部类检查。 我想,通过在Display_Activity创建的目的是访问code编写的内部类检查。
据我,我可以运行code中的内部类,但共享preference是从什么地方获得空
内 当我刷卡刷新,该应用程序会产生一个空对象异常。我不能让我在哪里提供空。我试图调试,但无法找到它。如果可以的话请帮帮忙。

The main activity has an inner class "check". I am trying to access the code written in the inner class "check", by creating its object in the Display_Activity.
According to me I am able to run the code inside the inner class but the Sharedpreference is getting null from somewhere
When I swipe to refresh, the application produces a null object exception. I am not able to get where I am supplying the null. I tried to debug but couldn't find it. Please help if you can.

我已经提供了这两个活动和logcat的。

I have provided with both the activities and the 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)

我是新来这个网站上,请给我时间来提高。请看到我的错误,如果它似乎不可思议,你可以简单地忽略它。谢谢你。

推荐答案

有相当多的问题,怎么回事,最相关的 上下文

There are quite a few issues going on here and most related to Context.

<一个href="http://stackoverflow.com/questions/7298731/when-to-call-activity-context-or-application-context/7298955#7298955">This回答讨论上下文链接的帖子在评论解释得很好也。

This answer discusses Context and a linked post in the comments explains it very well also.

有不同类型的上下文的S是在这里使用时,每一个应该只使用活动上下文(即 ActivityName.this 或你想要去买一台保持的活动的<$ C任何其他方式$ C>上下文。

There are different types of Contexts being used here when each should just be using the Activity Context (i.e. ActivityName.this, this, or any other way you want to go about getting a hold of the Activity's Context.

根本的问题是,你正试图从另一个引用的AsyncTask 内部类中的一个活动的。您正试图实例化活动新MainActivity()您应该永远做。

The underlying problem is that you are trying to reference the AsyncTask inner class of one Activity from another. You are trying to instantiate the Activity with new MainActivity() which you should never do.

由于任务需要在多个活动,简单地把它拿出来,使它自己的类。您可以创建一个构造函数来传递一个url变量或任何PARAMS您需要包括活动上下文

Since the task is needed in multiple activities, simply take it out and make it its own class. You can create a constructor to pass in a url variable or whatever params you need including the Activity Context.

此外,您可以使用上下文语境=这一点; 的onCreate()运行(您有它的外方法)。这将导致,因为上下文不可用 <$ C $前C>的onCreate()。

Also, you use Context context = this; before onCreate() runs (you have it outside of a method). This will result in null because the Context isn't available before onCreate().

这篇关于在刷卡刷新错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆