Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/183.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 刷卡刷新时出错_Java_Android - Fatal编程技术网

Java 刷卡刷新时出错

Java 刷卡刷新时出错,java,android,Java,Android,我有两项活动- 1. main活动 2. 显示活动 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 Main

我有两项活动-
1. main活动
2. 显示活动

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);
}
}
主活动有一个内部类“check”。 我试图通过在Display_活动中创建其对象来访问内部类“check”中编写的代码。
根据我的说法,我可以在内部类中运行代码,但是SharedReference从某个地方变为null
当我刷卡刷新时,应用程序会产生一个空对象异常。我无法获得提供空值的位置。我试图调试,但找不到。如果可以的话,请帮忙

我已经提供了活动和日志

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();
}}
刷卡刷新时的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)

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

这里有很多问题,而且与此最相关

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();
}}
讨论
上下文
,并对其进行了很好的解释

这里使用了不同类型的
上下文
,而每种上下文都应该只使用
活动上下文
(即
活动名称。此
,或者您想要掌握活动的
上下文
的任何其他方式)

潜在的问题是,您试图从一个
活动
引用另一个
活动的
AsyncTask
内部类。您试图用
new main Activity()
实例化
活动

由于任务在多个活动中都是必需的,只需将其取出并使其成为自己的类。您可以创建一个构造函数来传入url变量或任何您需要的参数,包括活动
上下文


另外,在运行
onCreate()
之前使用
Context-Context=this;
会导致
null
,因为
Context
onCreate()
之前不可用你问了一个令人惊讶的问题。`intent=new intent(???,A2.class)“
??”
需要上下文。将您的
MYACTIVITY放入其中。此
P.S.MYACTIVITY是您的活动名称寻求调试帮助的问题(“此代码为什么不起作用?”)必须包括所需的行为、特定的问题或错误以及在问题本身中重现问题所需的最短代码。没有明确问题说明的问题对其他读者没有用处。请参阅:ok。谢谢。@Murtakhursheedhussain当然不起作用,这就是为什么在此处发布的原因。您应该使用
活动上下文用于
Toast
MainActivity.this
)。此外,您的
上下文
变量为
null
,因为您尝试在
onCreate()
之前初始化它。您永远不希望使用
MainActivity MainActivity=new MainActivity()实例化活动
。如果您在多个活动中需要另一个类,则应将其作为单独的类。您甚至不需要检查
类。将AsyncTask作为自己的类,并创建一个构造函数来接受url(或您可能需要的任何参数)