0

我有一个扩展 Activity 的类,一切都很好!但我需要访问其中之一

此类中的函数,我无法将其定义为静态。....所以我制作了 Class 的副本(那个

扩展Activiy)并访问我的功能。如下 :

Content content = new Content();

但在那之后我需要使用setContentView(R.layout.post_content);.but 应用程序崩溃

这条线。我认为那是因为我创建了Contetn.class.

感谢您的帮助....


package com.example.mysqlex;

import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.net.URL;
import java.util.ArrayList;
import java.util.Arrays;

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

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.database.sqlite.SQLiteDatabase;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.graphics.Matrix;
import android.graphics.Typeface;
import android.graphics.drawable.Drawable;
import android.net.Uri;
import android.opengl.Visibility;
import android.os.AsyncTask;
import android.os.Bundle;
import android.text.Html;
import android.text.Layout;
import android.util.Base64;
import android.util.Log;
import android.util.TypedValue;
import android.view.Display;
import android.view.Gravity;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup.LayoutParams;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ImageView.ScaleType;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;
import android.content.Context;
import android.content.pm.PackageManager;

@SuppressLint("ResourceAsColor")
public class Content extends Activity {
static String Totalresult,catid;
static LinearLayout mainRel,popupRel;
static TextView title;
private static Context context;
private Typeface font;
Animation rotate,popupAnim,popupAnimHide,zoom_in;
public static Bitmap post_img_result;
public RelativeLayout popupLayout;
public Button closePopup_btn;

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

public boolean onKeyDown(int keyCode, KeyEvent event, Menu menu)  
{  
    //replaces the default 'Back' button action  
    if(keyCode==KeyEvent.KEYCODE_BACK){  
        super.onBackPressed();
        overridePendingTransition( R.anim.move_left, R.anim.move_right );
    }
    return true;  
}

   public String BitMapToString(Bitmap bitmap){
    ByteArrayOutputStream baos=new  ByteArrayOutputStream();
    bitmap.compress(Bitmap.CompressFormat.PNG,100, baos);
    byte [] b=baos.toByteArray();
    String temp=Base64.encodeToString(b, Base64.DEFAULT);
    return temp;
}
   public Bitmap StringToBitMap(String encodedString){
         try{
           byte [] encodeByte=Base64.decode(encodedString,Base64.DEFAULT);
               Bitmap bitmap=BitmapFactory.decodeByteArray(encodeByte, 0,    encodeByte.length);
               return bitmap;
             }catch(Exception e){
               e.getMessage();
               return null;
        }
    }

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.content); 
    context=this;
    catid="catid"+this.getIntent().getStringExtra("catid");
    mainRel=(LinearLayout) findViewById(R.id.mainRel);
    rotate = AnimationUtils.loadAnimation(getApplicationContext(),R.anim.rotate);
    zoom_in = AnimationUtils.loadAnimation(getApplicationContext(),R.anim.zoom_in);
    popupAnim = AnimationUtils.loadAnimation(getApplicationContext(),R.anim.popup);
    popupAnimHide = AnimationUtils.loadAnimation(getApplicationContext(),R.anim.popuphide);
    if(dbHandler.getSettings("fontFamily", "value").equals("yekan2")){
        font = Typeface.createFromAsset(getAssets(),"fonts/yekan2.TTF");
    }else if(dbHandler.getSettings("fontFamily", "value").equals("yekan1")){
        font = Typeface.createFromAsset(getAssets(),"fonts/yekan1.TTF");
    }else{
        font = Typeface.createFromAsset(getAssets(),"fonts/yekan2.TTF");
    }
    title=(TextView) findViewById(R.id.title);
    TextView app_title=(TextView) findViewById(R.id.app_title);
    app_title.setTypeface(font); title.setTypeface(font); title.setText(this.getIntent().getStringExtra("pageTitle"));
    if(dbHandler.getSettings(catid, "value")=="null"){
        Button btn = new Button(this);
        LinearLayout Llayout = new LinearLayout(this);
        btn.setBackgroundResource(R.drawable.post_drawable);
        btn.setTextSize(TypedValue.COMPLEX_UNIT_DIP,19); btn.setTypeface(font); btn.setTextColor(R.color.white);
        btn.setTag("noDataBtn");
        btn.setOnClickListener(new OnClickListener() {
        public void onClick(View arg0) {
            refresh_btn(arg0);
        }
    });

        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, 15, 0, 5);
        btn.setLayoutParams(btnLp);
        Llayout.setLayoutParams(LLp); Llayout.setGravity(0);

        btn.setText(R.string.noDataMsg);
        Llayout.addView(btn);
        mainRel.addView(Llayout);
    }else{
        refresh("home");
    }

}
public void refresh(final String refreshType){
    dbHandler=new DbHandler(context);
    dbHelper=new DbHelper(context);

    if(refreshType=="button"){
        if(dbHandler.getSettings(catid, "value").equals("null")){
            dbHandler.insertSettings(catid, Totalresult, "");
        }else{
            dbHandler.updateSettings(catid, Totalresult, "");
        }
    }else if(refreshType=="home"){
        Totalresult=dbHandler.getSettings(catid, "value");
    }

    if(dbHandler.getSettings("fontFamily", "value").equals("yekan2")){
        font = Typeface.createFromAsset(context.getAssets(),"fonts/yekan2.TTF");
    }else if(dbHandler.getSettings("fontFamily", "value").equals("yekan1")){
        font = Typeface.createFromAsset(context.getAssets(),"fonts/yekan1.TTF");
    }else{
        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[] title=parts[1].split("->");

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

        LinearLayout post = new LinearLayout(context); post.setOrientation(LinearLayout.VERTICAL);
        post.setBackgroundResource(R.drawable.post_drawable);
        RelativeLayout.LayoutParams post_Rparams = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.FILL_PARENT,RelativeLayout.LayoutParams.WRAP_CONTENT);
        LinearLayout.LayoutParams post_Lparams = new LinearLayout.LayoutParams(RelativeLayout.LayoutParams.FILL_PARENT,RelativeLayout.LayoutParams.WRAP_CONTENT);

        post_Lparams.setMargins(0, 10, 0, 10);
        post.setLayoutParams(post_Lparams);

        //post title Layout
        LinearLayout titleLayout = new LinearLayout(context);
        titleLayout.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT, 80));
        titleLayout.setBackgroundResource(R.drawable.post_title);
        post.addView(titleLayout);

        //post title text
        TextView post_title = new TextView(context);
        LinearLayout.LayoutParams post_titleParams = new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT);
        post_titleParams.gravity=Gravity.CENTER_VERTICAL | Gravity.RIGHT;
        post_title.setLayoutParams(post_titleParams);
        post_title.setText(" :: "+title[1]);
        post_title.setTextColor(Color.parseColor("#ffffff")); post_title.setTypeface(font); post_title.setTextSize(TypedValue.COMPLEX_UNIT_DIP,19);
        titleLayout.addView(post_title);

        //post content bg
        LinearLayout contentL = new LinearLayout(context); contentL.setOrientation(LinearLayout.HORIZONTAL); contentL.setPadding(20, 30, 20, 30);
        contentL.setBackgroundResource(R.drawable.post_drawable);
        LinearLayout.LayoutParams post_contentParams = new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT);
        contentL.setLayoutParams(post_contentParams);

        post.addView(contentL);

        //post image
        LinearLayout rightColumn=new LinearLayout(context); rightColumn.setOrientation(LinearLayout.VERTICAL);
        LinearLayout.LayoutParams rightColumnP = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.FILL_PARENT);
        rightColumn.setLayoutParams(rightColumnP);

        ImageView post_img = new ImageView(context);
        if(image.length>1){
            String imgUrl = SigninActivity.url_2+image[1].toString();
            if(refreshType=="button"){
                new DownloadImageTask(post_img).execute(imgUrl,id[1],"button");
            }else{
              String img_name1="img"+id[1];
              if(dbHandler.getSettings(img_name1, "value").equals("null")){
                  new DownloadImageTask(post_img).execute(imgUrl,id[1],"button");
              }else{
                  Bitmap bitmap=StringToBitMap(dbHandler.getSettings(img_name1, "value"));
                  post_img.setImageBitmap(bitmap);
              }
            }
        }else{
            post_img.setImageResource(R.drawable.noimg_img);
        }
        LinearLayout.LayoutParams post_imgParams = new LinearLayout.LayoutParams(230, 230);
        post_imgParams.gravity=Gravity.RIGHT; post_img.setScaleType(ScaleType.CENTER_CROP); post_imgParams.weight=1;
        post_img.setLayoutParams(post_imgParams);
        rightColumn.addView(post_img);

        //post Intro Text
        LinearLayout leftColumn=new LinearLayout(context); leftColumn.setOrientation(LinearLayout.VERTICAL);
        LinearLayout.LayoutParams leftColumnP = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.FILL_PARENT);
        leftColumn.setLayoutParams(leftColumnP);

        TextView post_text = new TextView(context);
        Integer lastInt=introText[1].substring(0, 150).lastIndexOf(" ");
        String postText1=introText[1].substring(0, lastInt)+" ... ";
        postText1= postText1.replaceAll("\\<.*?>", "");
        post_text.setText(Html.fromHtml(postText1).toString());

        post_text.setTypeface(font); post_text.setTextSize(TypedValue.COMPLEX_UNIT_DIP,15); post_text.setTextColor(Color.parseColor("#3F3F3F")); 
        LinearLayout.LayoutParams post_textParams = new LinearLayout.LayoutParams(400, 200);
        post_textParams.gravity=Gravity.LEFT; post_textParams.weight=1; post_textParams.setMargins(0, 0, 15, 0);
        post_text.setLayoutParams(post_textParams);
        leftColumn.addView(post_text);

        //post MoreText Buttton

        final Button moreBtn = new Button(context);
        moreBtn.setId(i);
        moreBtn.setBackgroundResource(R.drawable.morebtn);
        moreBtn.setText(R.string.readMore); moreBtn.setTypeface(font); moreBtn.setTextColor(Color.parseColor("#ffffff")); moreBtn.setTextSize(TypedValue.COMPLEX_UNIT_DIP,16);
        LinearLayout.LayoutParams moreBtnParams = new LinearLayout.LayoutParams(230, 77);
        moreBtnParams.gravity=Gravity.RIGHT;
        moreBtnParams.setMargins(0, 10,0,0);
        moreBtn.setLayoutParams(moreBtnParams);

        rightColumn.addView(moreBtn);

        //post Share Buttton
        Button shareBtn = new Button(context);
        shareBtn.setBackgroundResource(R.drawable.morebtn);
        shareBtn.setCompoundDrawablesWithIntrinsicBounds(0, R.drawable.share,0 , 0);
        LinearLayout.LayoutParams shareBtnParams = new LinearLayout.LayoutParams(77, 77);
        shareBtnParams.gravity=Gravity.LEFT;
        shareBtnParams.setMargins(0, 10, 0, 0);
        shareBtn.setLayoutParams(shareBtnParams);
        leftColumn.addView(shareBtn);

        shareBtn.setOnClickListener(new OnClickListener() {
            public void onClick(View arg0) {
                Intent sharingIntent = new Intent(android.content.Intent.ACTION_SEND);
                sharingIntent.setType("text/html");
                sharingIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, title[1]);
                sharingIntent.putExtra(android.content.Intent.EXTRA_TEXT, "<!DOCTYPE html><html><head><title>"+Html.fromHtml(title[1])+"</title></head><body>"+Html.fromHtml(introText[1])+"</body></html>");
                startActivity(Intent.createChooser(sharingIntent, "Share via"));
              }
            });

        contentL.addView(rightColumn);
        contentL.addView(leftColumn);          

        //right to left contentL :
        ArrayList<View> views = new ArrayList<View>();
        for(int x = 0; x < contentL.getChildCount(); x++) {
            views.add(contentL.getChildAt(x));
        }
        contentL.removeAllViews();
        for(int x = views.size() - 1; x >= 0; x--) {
            contentL.addView(views.get(x));
        }


        mainRel.addView(post);
        moreBtn.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View arg0) {
                setContentView(R.layout.post_content);
                popupRel=(LinearLayout) findViewById(R.id.popupRel);
              /*popupLayout=(RelativeLayout) findViewById(R.id.popupLayout);
              popupLayout.startAnimation(popupAnim);
              popupLayout.setVisibility(View.VISIBLE);*/
              popupRel.removeAllViews();
              //image
              ImageView post_imgP = new ImageView(context);
                if(image.length>1){
                    String imgUrl = SigninActivity.url_2+image[1].toString();
                if(refreshType=="button"){
                    new DownloadImageTask(post_imgP).execute(imgUrl,id[1],"button");
                }else{
                  String img_name1="img"+id[1];
                  if(dbHandler.getSettings(img_name1, "value").equals("null")){
                          new DownloadImageTask(post_imgP).execute(imgUrl,id[1],"button");
                  }else{
                      Bitmap bitmap=StringToBitMap(dbHandler.getSettings(img_name1, "value"));
                      post_imgP.setImageBitmap(bitmap);
                  }
                }
                }else{
                  post_imgP.setImageResource(R.drawable.noimg_img);
                }
              LinearLayout.LayoutParams post_img_paramsPopup=new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT , 400);
                post_imgP.setScaleType(ScaleType.FIT_CENTER);
                post_imgP.setLayoutParams(post_img_paramsPopup);

              popupRel.addView(post_imgP);

            //post Intro Text
                TextView post_textP = new TextView(context);
                post_textP.setText(Html.fromHtml(introText[1]));

                post_textP.setTypeface(font); post_textP.setTextSize(TypedValue.COMPLEX_UNIT_DIP,18); post_textP.setTextColor(Color.parseColor("#FFFFFF")); 
                LinearLayout.LayoutParams post_textParams2 = new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT);
                post_textParams2.setMargins(15, 20, 15, 0);
                post_textP.setLayoutParams(post_textParams2);
                popupRel.addView(post_textP);
            }
          }); 
    }

}

    public void closeBtn(View arg0) {
      popupLayout.startAnimation(popupAnimHide);
      popupLayout.setVisibility(View.GONE);
    }

public void post_img_resfresh(ImageView img, Bitmap bitmap, String id, String refresh_type){
    dbHandler=new DbHandler(context);
    dbHelper=new DbHelper(context);
    String img_name="img"+id;

    if(refresh_type=="button"){
      if(dbHandler.getSettings(img_name, "value").equals("null")){
          dbHandler.insertSettings(img_name,BitMapToString(bitmap) , "");
          bitmap=StringToBitMap(dbHandler.getSettings(img_name, "value"));
      }else{
          dbHandler.updateSettings(img_name,BitMapToString(bitmap) , "");
          bitmap=StringToBitMap(dbHandler.getSettings(img_name, "value"));
      }
    }else if(refresh_type=="home"){
          bitmap=StringToBitMap(dbHandler.getSettings(img_name, "value"));

    }
    img.setImageBitmap(bitmap);
}
public void refresh_btn(View v) {
    if(v.getTag()!="noDataBtn"){
        v.startAnimation(rotate);
    }
        if(Main.haveNetworkConnection()){
            SigninActivity.classReferrer="Content";
            new SigninActivity(this,0).execute("site_id","1","catid",this.getIntent().getStringExtra("catid"),"data_type","echo(/.../)");
        }else{
            Toast.makeText(getApplicationContext(), R.string.noInternetConnection, Toast.LENGTH_LONG).show();
        }
}
public void exit_btn(View v) {
    finish();
}
public void settings_btn(View v) {
    Intent goToSettings = new Intent(v.getContext(), Settings.class); 
    v.getContext().startActivity(goToSettings);
    ((Activity) v.getContext()).overridePendingTransition( R.anim.zoom_out,R.anim.zoom_in );
}
public void about_btn(View v) {
    Intent goToAbout = new Intent(v.getContext(), About.class); 
    v.getContext().startActivity(goToAbout);
    ((Activity) v.getContext()).overridePendingTransition( R.anim.zoom_out,R.anim.zoom_in );
}
public void back_btn(View v) {
    Intent goToMain = new Intent(Content.this, Main.class); 
    startActivity(goToMain);
    overridePendingTransition( R.anim.move_left, R.anim.move_right );
}
public boolean onCreateOptionsMenu(Menu menu) {

    MenuInflater inflater = getMenuInflater();

    inflater.inflate(R.menu.menu, menu);

    return true;

}
public boolean onOptionsItemSelected(MenuItem item) {

    switch (item.getItemId()) {

    case R.id.about:

        Intent goToAbout = new Intent(Content.this, About.class); 
        startActivity(goToAbout);
        overridePendingTransition( R.anim.zoom_out,R.anim.zoom_in );

    return true;

    case R.id.refresh:

        if(Main.haveNetworkConnection()){
            SigninActivity.classReferrer="Content";
            new SigninActivity(this,0).execute("site_id","1","catid",this.getIntent().getStringExtra("catid"),"echo(/.../)");
        }else{
            Toast.makeText(getApplicationContext(), R.string.noInternetConnection, Toast.LENGTH_LONG).show();
        }

    return true;

    case R.id.help:

        Intent goToHelp = new Intent(Content.this, Help.class); 
        startActivity(goToHelp);
        overridePendingTransition( R.anim.zoom_out,R.anim.zoom_in );

    return true;

    case R.id.exit:

        android.os.Process.killProcess(android.os.Process.myPid());
        System.exit(1);

    return true;

    default:

    return super.onOptionsItemSelected(item);

    }

    }
}

`08-16 13:43:33.429: E/Trace(22297): error opening trace file: No such file or directory (2)
08-16 13:44:18.127: E/AndroidRuntime(22297): FATAL EXCEPTION: main

08-16 13:44:18.127: E/AndroidRuntime(22297): java.lang.NullPointerException

08-16 13:44:18.127: E/AndroidRuntime(22297):    at 
android.app.Activity.setContentView(Activity.java:1912)

08-16 13:44:18.127: E/AndroidRuntime(22297):    at com.example.mysqlex.Content$3.onClick(Content.java:306)

08-16 13:44:18.127: E/AndroidRuntime(22297):    at android.view.View.performClick(View.java:4102)

08-16 13:44:18.127: E/AndroidRuntime(22297):    at 
android.view.View$PerformClick.run(View.java:17085)

08-16 13:44:18.127: E/AndroidRuntime(22297):    at android.os.Handler.handleCallback(Handler.java:615)

08-16 13:44:18.127: E/AndroidRuntime(22297):    at android.os.Handler.dispatchMessage(Handler.java:92)

08-16 13:44:18.127: E/AndroidRuntime(22297):    at android.os.Looper.loop(Looper.java:155)

08-16 13:44:18.127: E/AndroidRuntime(22297):    at android.app.ActivityThread.main(ActivityThread.java:5511)

08-16 13:44:18.127: E/AndroidRuntime(22297):    at 
java.lang.reflect.Method.invokeNative(Native Method)

08-16 13:44:18.127: E/AndroidRuntime(22297):    at java.lang.reflect.Method.invoke(Method.java:511)

08-16 13:44:18.127: E/AndroidRuntime(22297):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1029)

08-16 13:44:18.127: E/AndroidRuntime(22297):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:796)

08-16 13:44:18.127: E/AndroidRuntime(22297):    at dalvik.system.NativeStart.main(Native Method)`
4

0 回答 0