0

actually i add some buttons programmatically with no problems.after that i need to recreate buttons and delete old buttons.also i had set OnClickListener for each button.my problem is that when i recall funcrion refresh() and recreate buttons,buttons added succefully,but when i click on buttons,program crashs! :|

      Main Class:

package com.example.mysqlex;

import com.example.mysqlex.DbHandler;
import com.example.mysqlex.DbHelper;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.database.sqlite.SQLiteDatabase;
import android.graphics.Typeface;
import android.graphics.drawable.Drawable;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup.LayoutParams;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.ScrollView;
import android.widget.TextView;
import android.widget.Toast;
import android.content.Context;

@SuppressLint("ResourceAsColor")
public class Main extends Activity {
static String Totalresult;
static LinearLayout Llayout,mainRel;
static TextView titleTest;
private static Context context;
private Typeface font;


private DbHandler dbHandler=new DbHandler(this);
public DbHelper dbHelper=new DbHelper(this);
private SQLiteDatabase database;

public static boolean haveNetworkConnection() {
    boolean haveConnectedWifi = false;
    boolean haveConnectedMobile = false;

    ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo[] netInfo = cm.getAllNetworkInfo();
    for (NetworkInfo ni : netInfo) {
        if (ni.getTypeName().equalsIgnoreCase("WIFI"))
            if (ni.isConnected())
                haveConnectedWifi = true;
        if (ni.getTypeName().equalsIgnoreCase("MOBILE"))
            if (ni.isConnected())
                haveConnectedMobile = true;
    }
    return haveConnectedWifi || haveConnectedMobile;
}
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main); 
    mainRel=(LinearLayout) findViewById(R.id.mainRel);
    Typeface font = Typeface.createFromAsset(getAssets(),"fonts/yekan2.TTF");
    TextView title=(TextView) findViewById(R.id.title);
    title.setTypeface(font);
    context=this;
    if(dbHandler.getSettings("catlist", "value")=="null"){
        Toast.makeText(getApplicationContext(), "اطلاعاتی ذخیره نشده است.لطفا نوسازی کنید...", 5000).show();
    }else{
        refresh("home");
    }
}
public void refresh(String refreshType){
    dbHandler=new DbHandler(context);
    dbHelper=new DbHelper(context);
    if(refreshType=="button"){
        if(dbHandler.getSettings("catlist", "value")=="null"){
            dbHandler.insertSettings("catlist", Totalresult, "");
        }else{
            dbHandler.updateSettings("catlist", Totalresult, "");
        }
    }else if(refreshType=="home"){
        Totalresult=dbHandler.getSettings("catlist", "value");
    }
    Typeface font = Typeface.createFromAsset(context.getAssets(),"fonts/yekan2.TTF");
    String[] childs=Totalresult.split("/.../<br />");
    mainRel.removeAllViews();

    for(int i=0; i<childs.length; i++){
        String[] parts=childs[i].split("<br />");
        final String[] values=parts[1].split("->");

        final String[] id=parts[0].split("->");

        Button btn = new Button(context);
        LinearLayout Llayout = new LinearLayout(context);
        btn.setBackgroundResource(R.drawable.cat_drawable);
        btn.setHeight(110); btn.setTextSize(24); btn.setTypeface(font); btn.setTextColor(R.color.white);
        btn.setId(i);
        btn.setOnClickListener(new OnClickListener() {
        public void onClick(View arg0) {
            Intent goToContent = new Intent(Main.this, Content.class); 
            goToContent.putExtra("catid", id[1]);
            goToContent.putExtra("pageTitle", values[1]);
            startActivity(goToContent);
        }
    });

        LayoutParams btnLp = new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT);
        LinearLayout.LayoutParams LLp = new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT);

        LLp.setMargins(0, 5, 0, 5);
        btn.setLayoutParams(btnLp);
        Llayout.setLayoutParams(LLp);

        btn.setText(values[1]);
        Llayout.addView(btn);
        mainRel.addView(Llayout);
    }
}
public void refresh_btn(View v) {
    if(haveNetworkConnection()){
        SigninActivity.classReferrer="Main";
        new SigninActivity(this,0).execute("site_id","1","catlist","1","data_type","echo(/.../)");
    }else{
        Toast.makeText(getApplicationContext(), "لطفا جهت نوسازی اطلاعات،به اینترنت متصل شوید", Toast.LENGTH_LONG).show();
    }
  }
}


   SigninActivity Class :
package com.example.mysqlex;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.lang.ref.WeakReference;
import java.lang.reflect.Array;
import java.net.URI;
import java.net.URL;
import java.net.URLConnection;
import java.net.URLEncoder;

import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;


import android.app.ProgressDialog;
import android.content.Context;
import android.os.AsyncTask;
import android.widget.TextView;
import android.widget.Toast;

public class SigninActivity  extends AsyncTask<String,Void,String>{
private ProgressDialog pdia;
String url="http://www.pbroilgas.com/index2.php";
private TextView roleField;
private Context context;
private int byGetOrPost = 0; 
static public String results,classReferrer;
static public String[] resultArray;
//flag 0 means get and 1 means post.(By default it is get.)
public SigninActivity(Context context,int flag) {
  this.context = context;
  byGetOrPost = flag;
}

protected void onPreExecute(){

   pdia = new ProgressDialog(context);
   pdia.setMessage("در حال نوسازی اطلاعات...");
   pdia.setProgressStyle(pdia.STYLE_SPINNER);
   pdia.show();
   super.onPreExecute();
}
@Override
protected String doInBackground(String... arg0) {
  if(byGetOrPost == 0){ //means by Get Method
     try{
         String command1 = (String)arg0[0];
         String value1 = (String)arg0[1];
         StringBuilder link = new StringBuilder();
         link.append(url+"?"+command1+"="+value1);
         for(int i=2; i<arg0.length; i+=2){
             link.append("&"+arg0[i]);
             link.append("="+arg0[i+1]);
         }
        URL url = new URL(link.toString());
        HttpClient client = new DefaultHttpClient();
        HttpGet request = new HttpGet();
        request.setURI(new URI(link.toString()));
        HttpResponse response = client.execute(request);
        BufferedReader in = new BufferedReader
       (new InputStreamReader(response.getEntity().getContent()));

       StringBuffer sb = new StringBuffer("");
       String line;
       while ((line = in.readLine()) != null) {
           sb.append(line);
        }
        in.close();
        return sb.toString();
  }catch(Exception e){
     return new String("Exception: " + e.getMessage());
  }
  }
  else{
     try{
        String command = (String)arg0[0];
        String value = (String)arg0[1];
        String link=url;
        String data  = URLEncoder.encode(command, "UTF-8") 
        + "=" + URLEncoder.encode(value, "UTF-8");
        URL url = new URL(link);
        URLConnection conn = url.openConnection(); 
        conn.setDoOutput(true); 
        OutputStreamWriter wr = new OutputStreamWriter
        (conn.getOutputStream()); 
        wr.write( data ); 
        wr.flush(); 
        BufferedReader reader = new BufferedReader
        (new InputStreamReader(conn.getInputStream()));
        StringBuilder sb = new StringBuilder();
        String line = null;
        // Read Server Response
        while((line = reader.readLine()) != null)
        {
           sb.append(line);
           break;
        }
        return sb.toString();
     }catch(Exception e){
        return new String("Exception: " + e.getMessage());
     }
  }
}

@Override
protected void onPostExecute(String result){
   pdia.dismiss();
   results=result;
   if(classReferrer=="Main"){
       Main  main= new Main();
       Main.Totalresult=result.toString();
       main.refresh("button");
   }else if(classReferrer=="Content"){
       Content  content= new Content();
       Content.Totalresult=result.toString();
       content.refresh("button");
   }
  }
}

my LogCat : 08-11 02:12:55.839: E/AndroidRuntime(8853): FATAL EXCEPTION: main 08-11 02:12:55.839: E/AndroidRuntime(8853): java.lang.NullPointerException 08-11 02:12:55.839: E/AndroidRuntime(8853): at android.content.ContextWrapper.getPackageName(ContextWrapper.java:127) 08-11 02:12:55.839: E/AndroidRuntime(8853): at android.content.ComponentName.<init>(ComponentName.java:75) 08-11 02:12:55.839: E/AndroidRuntime(8853): at android.content.Intent.<init>(Intent.java:3350) 08-11 02:12:55.839: E/AndroidRuntime(8853): at com.example.mysqlex.Main$1.onClick(Main.java:99) 08-11 02:12:55.839: E/AndroidRuntime(8853): at android.view.View.performClick(View.java:4147) 08-11 02:12:55.839: E/AndroidRuntime(8853): at android.view.View$PerformClick.run(View.java:17161) 08-11 02:12:55.839: E/AndroidRuntime(8853): at android.os.Handler.handleCallback(Handler.java:615) 08-11 02:12:55.839: E/AndroidRuntime(8853): at android.os.Handler.dispatchMessage(Handler.java:92) 08-11 02:12:55.839: E/AndroidRuntime(8853): at android.os.Looper.loop(Looper.java:213) 08-11 02:12:55.839: E/AndroidRuntime(8853): at android.app.ActivityThread.main(ActivityThread.java:4786) 08-11 02:12:55.839: E/AndroidRuntime(8853): at java.lang.reflect.Method.invokeNative(Native Method) 08-11 02:12:55.839: E/AndroidRuntime(8853): at java.lang.reflect.Method.invoke(Method.java:511) 08-11 02:12:55.839: E/AndroidRuntime(8853): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:789) 08-11 02:12:55.839: E/AndroidRuntime(8853): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:556) 08-11 02:12:55.839: E/AndroidRuntime(8853): at dalvik.system.NativeStart.main(Native Method) i appreciate your helps... thanks in advance...

14
  • childs.length does that have a value? Its a null pointer exception...so probably that is null? Commented Aug 10, 2014 at 21:48
  • thanks for reply bro...yes for sure.my proplem is onClickListener that doesnt works for the seccond time that is set for btn Commented Aug 10, 2014 at 21:50
  • @gurinderhans if it was null,so the button views shouldn't be added to the view..right ? so its no null... Commented Aug 10, 2014 at 21:52
  • Thats right! Can you specify which line it gives the error at? Commented Aug 10, 2014 at 21:52
  • 1
    Silly question but is that refresh(...) method part of an Activity called Main? Commented Aug 10, 2014 at 22:02

2 Answers 2

1

The error says that it can't get the package name. So your Main.this is invalid at that point for some reason. Change it to this:

    public void onClick(View arg0) {
        Intent goToContent = new Intent(arg0.getContext(), Content.class);
        goToContent.putExtra("catid", id[1]);
        goToContent.putExtra("pageTitle", values[1]);
        startActivity(goToContent);
    }

You have an invalid context reference, but the view that calls the method should never be invalid.

Sign up to request clarification or add additional context in comments.

6 Comments

thank you for your answer my friend. i did as you said,the app crashs again,but logCat changed as below:
08-11 02:50:49.755: E/AndroidRuntime(14203): FATAL EXCEPTION: main 08-11 02:50:49.755: E/AndroidRuntime(14203): java.lang.NullPointerException 08-11 02:50:49.755: E/AndroidRuntime(14203): at android.app.Activity.startActivityForResult(Activity.java:3351) 08-11 02:50:49.755: E/AndroidRuntime(14203): at android.app.Activity.startActivityForResult(Activity.java:3312) 08-11 02:50:49.755: E/AndroidRuntime(14203): at android.app.Activity.startActivity(Activity.java:3522) 08-11 02:50:49.755: E/AndroidRuntime(14203): at android.app.Activity.startActivity(Activity.java:3490)
08-11 02:50:49.755: E/AndroidRuntime(14203): at com.example.mysqlex.Main$1.onClick(Main.java:102) 08-11 02:50:49.755: E/AndroidRuntime(14203): at android.view.View.performClick(View.java:4147) 08-11 02:50:49.755: E/AndroidRuntime(14203): at android.view.View$PerformClick.run(View.java:17161) 08-11 02:50:49.755: E/AndroidRuntime(14203): at android.os.Handler.handleCallback(Handler.java:615) 08-11 02:50:49.755: E/AndroidRuntime(14203): at android.os.Handler.dispatchMessage(Handler.java:92) 08-11 02:50:49.755: E/AndroidRuntime(14203): at
android.os.Looper.loop(Looper.java:213) 08-11 02:50:49.755: E/AndroidRuntime(14203): at android.app.ActivityThread.main(ActivityThread.java:4786) 08-11 02:50:49.755: E/AndroidRuntime(14203): at java.lang.reflect.Method.invokeNative(Native Method) 08-11 02:50:49.755: E/AndroidRuntime(14203): at java.lang.reflect.Method.invoke(Method.java:511) 08-11 02:50:49.755: E/AndroidRuntime(14203): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:789) 08-11 02:50:49.755: E/AndroidRuntime(14203): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:556)
Now you have an error at line 102 - "startActivity" - that means the activity you are referencing when that method is called is null; as suspected, "Main" is dereferenced at this point. Try "arg0.getContext().startActivity(goToContent)"
|
0

Looking at the source for the Intent(Context, Class<?>)...

public Intent(Context packageContext, Class<?> cls) {
    mComponent = new ComponentName(packageContext, cls);
}

...then looking at the source for the ComponentName(Context, Class<?>) constructor...

public ComponentName(Context pkg, Class<?> cls) {
    mPackage = pkg.getPackageName();
    mClass = cls.getName();
}

...the only way using Intent(Context, Class<?>) can throw an NPE is at mPackage = pkg.getPackageName(); - in other words Main.this is null.

Looking at your updated code I can see why. In onPostExecute(...) of the AsyncTask in your SigninActivity you are doing the following...

Main main= new Main();

It is not possible to create an instance of an Activity using new - it simply doesn't work and that's why Main.this is null.

You're going to have to rethink things as you should never attempt to access any variables or methods of an Activity from any other app component.

If you need to access methods in an Activity from an AsyncTask, the easiest thing is to make the AsyncTask an inner class of the Activity itself.

3 Comments

ok, i wil change that part! but how can i call refresh() which is in another class,according to that i cant set refresh() to static, because of its inside codes...
Put your AsyncTask code as an inner class of your Main Activity. In that way the onPostExecute(...) method can directly call the refresh(...) method.
oh yes..! your right...it was a good idea my friend.thank you for your help <3

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.