Android:应用程序在单击快速操作项时崩溃

时间:2012-10-31 02:28:54

标签: android android-asynctask quickaction

在我的应用中,我在单个标签下打开多个活动。在其中一个标签中,我有快速行动。我想点击快速操作的项目打开另一个活动,但是当我这样做时应用程序崩溃了。我使用Activity组来维护一个活动下的多个活动。我的第一个标签包含listview。在项目单击列表视图时,我正在打开另一个活动。我想用Quick action的项目点击打开同样的活动。我在下面发布我的代码。

快速行动准则

quickAction = new QuickAction(this, QuickAction.VERTICAL);

    ActionItem nextItem     = new ActionItem(ID_DOWN, "About TIE", getResources().getDrawable(R.drawable.dzologo));
    ActionItem prevItem     = new ActionItem(ID_UP, "About LKS", getResources().getDrawable(R.drawable.dzologo));

        prevItem.setSticky(true);
        nextItem.setSticky(true);

      final QuickAction quickAction = new QuickAction(this, QuickAction.VERTICAL);

        //add action items into QuickAction
        quickAction.addActionItem(nextItem);
        quickAction.addActionItem(prevItem);

//Set listener for action item clicked
        quickAction.setOnActionItemClickListener(new QuickAction.OnActionItemClickListener() {

            public void onItemClick(com.dzo.tie.quickaction.QuickAction source,
                    int pos, int actionId) {

                if(actionId == ID_DOWN)
                {
                    Intent  iInfo = new Intent(Awesome.this, TIEInfo.class);
                    startActivity(iInfo);
                    quickAction.dismiss();
                }//if
            }           
        });

添加tabspec的代码

addTab("Home", R.drawable.tab_home, TabGroup1Activity.class);

//Add tab method
private void addTab(String labelId, int drawableId, Class<?> c)
{
    Intent intent = null;
    intent = new Intent(this, c);

    TabHost.TabSpec spec = tabHost.newTabSpec("tab" + labelId); 
    View tabIndicator = LayoutInflater.from(this).inflate(R.layout.tab_indicator, getTabWidget(), false);
    TextView title = (TextView) tabIndicator.findViewById(R.id.title);
    title.setText(labelId);
    ImageView icon = (ImageView) tabIndicator.findViewById(R.id.icon);
    icon.setImageResource(drawableId);      
    spec.setIndicator(tabIndicator);    
    spec.setContent(intent);

    tabHost.addTab(spec);   
}

Tabgroup1活动

public class TabGroup1Activity extends TabGroupActivity{

@Override
public void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    startChildActivity("MenuActivity", new Intent(getParent(), MenuActivity.class));
}
}

OnItem点击菜单活动中的列表视图

    public void onItemClick(AdapterView<?> parent, View v, int pos, long id)
{
    handleClick(pos);
}

private void handleClick(int position) 
{
    if(position==0)
    {
        Intent  tieInfoIntent = new Intent(getParent(), TIEInfo.class);
        TabGroupActivity parentActivity = (TabGroupActivity)getParent();
        parentActivity.startChildActivity("TIEInfoActivity", tieInfoIntent);
    }//if
}      

TIEInfo 的 我希望在项目点击快速操作时打开此活动。

public class TIEInfo extends Activity 
{
WebView webTieInfo;
String contents;
HeaderBar headerbar;
private String infoUrl = "https://www.tradeineu.com/tie_app/aboutTie.php";
protected void onCreate(Bundle savedInstanceState) 
{
    super.onCreate(savedInstanceState);
    init();
        new InfoAsyncTask(getParent(), infoUrl, webTieInfo).execute();
}//onCreate

private void init()
{
    setContentView(R.layout.tieinfo);
    headerbar = (HeaderBar)findViewById(R.id.headerBar);
    headerbar.setTitle("Info");
    webTieInfo = (WebView)findViewById(R.id.webTieInfo);
    webTieInfo.setVerticalFadingEdgeEnabled(true);
    webTieInfo.setVerticalScrollBarEnabled(true);
    webTieInfo.setScrollBarStyle(View.SCROLLBARS_OUTSIDE_OVERLAY);
}//init
}//TIEInfo

Logcat追踪

10-31 07:50:49.148: E/AndroidRuntime(1012): FATAL EXCEPTION: main
10-31 07:50:49.148: E/AndroidRuntime(1012): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.dzo.tie/com.dzo.tie.TIEInfo}: java.lang.NullPointerException
10-31 07:50:49.148: E/AndroidRuntime(1012):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2059)
10-31 07:50:49.148: E/AndroidRuntime(1012):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084)
10-31 07:50:49.148: E/AndroidRuntime(1012):     at android.app.ActivityThread.access$600(ActivityThread.java:130)
10-31 07:50:49.148: E/AndroidRuntime(1012):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195)
10-31 07:50:49.148: E/AndroidRuntime(1012):     at android.os.Handler.dispatchMessage(Handler.java:99)
10-31 07:50:49.148: E/AndroidRuntime(1012):     at android.os.Looper.loop(Looper.java:137)
10-31 07:50:49.148: E/AndroidRuntime(1012):     at android.app.ActivityThread.main(ActivityThread.java:4745)
10-31 07:50:49.148: E/AndroidRuntime(1012):     at java.lang.reflect.Method.invokeNative(Native Method)
10-31 07:50:49.148: E/AndroidRuntime(1012):     at java.lang.reflect.Method.invoke(Method.java:511)
10-31 07:50:49.148: E/AndroidRuntime(1012):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
10-31 07:50:49.148: E/AndroidRuntime(1012):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
10-31 07:50:49.148: E/AndroidRuntime(1012):     at dalvik.system.NativeStart.main(Native Method)
10-31 07:50:49.148: E/AndroidRuntime(1012): Caused by: java.lang.NullPointerException
10-31 07:50:49.148: E/AndroidRuntime(1012):     at android.app.AlertDialog.resolveDialogTheme(AlertDialog.java:142)
10-31 07:50:49.148: E/AndroidRuntime(1012):     at android.app.AlertDialog.<init>(AlertDialog.java:98)
10-31 07:50:49.148: E/AndroidRuntime(1012):     at android.app.ProgressDialog.<init>(ProgressDialog.java:77)
10-31 07:50:49.148: E/AndroidRuntime(1012):     at com.dzo.tie.asynctask.InfoAsyncTask.onPreExecute(InfoAsyncTask.java:43)
10-31 07:50:49.148: E/AndroidRuntime(1012):     at android.os.AsyncTask.executeOnExecutor(AsyncTask.java:586)
10-31 07:50:49.148: E/AndroidRuntime(1012):     at android.os.AsyncTask.execute(AsyncTask.java:534)
10-31 07:50:49.148: E/AndroidRuntime(1012):     at com.dzo.tie.TIEInfo.onCreate(TIEInfo.java:31)
10-31 07:50:49.148: E/AndroidRuntime(1012):     at android.app.Activity.performCreate(Activity.java:5008)
10-31 07:50:49.148: E/AndroidRuntime(1012):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079)
10-31 07:50:49.148: E/AndroidRuntime(1012):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2023) 
10-31 07:50:49.148: E/AndroidRuntime(1012):     ... 11 more

从日志中,我开始知道应用程序崩溃在下面的语句中,该语句位于AsyncTask的onPreExecute()中。但是,我无法指出实际原因。

pd = new ProgressDialog(ctx);

异步任务

public class InfoAsyncTask extends AsyncTask<Void, Void, JSONObject>
{
Context ctx;
ProgressDialog pd; 
JSONParser jsonParser;
String url;
WebView webInfo;
public static TieInfoDAO tieInfoDao;
public static ComplianceInfoDAO complianceInfoDAO;
public static TAXInfoDAO taxDAO;
public static LogisticsDAO logisticsDAO;
public static LegalDAO legalDAO;
public static SmartAppDAO smartAppDAO;
private final String TAG = "InfoAsyncTask";

public InfoAsyncTask(Context ctx, String url, WebView webInfo) 
{
    this.ctx = ctx;
    this.url = url;
    this.webInfo = webInfo;
}//Constructor

@Override
protected void onPreExecute() 
{
    pd = new ProgressDialog(ctx);
    pd.setMessage("Please wait...");
    pd.show();
    Log.v(TAG, "onPreExecute called");
}//onPreExecute

protected JSONObject doInBackground(Void... params) 
{
    jsonParser = new JSONParser();
    JSONObject jsonObject = jsonParser.getJSONFromUrl(url);
    Log.v(TAG, "doInbackground called");
    return jsonObject;
}//doINBackground

@Override
protected void onPostExecute(JSONObject jsonObject) 
{
    String contents = null;

    if(url.contains("aboutTie"))
    {
        tieInfoDao = ParsedInfoData.getTIEInfo(jsonObject);
        contents = tieInfoDao.getInfo();
        showWebView(contents);
    }//if   
    else if(url.contains("compliance"))
    {
        complianceInfoDAO = ParsedComplianceInfoData.getComplianceInfo(jsonObject);
        contents = complianceInfoDAO.getComplianceInfo();
        showWebView(contents);
    }//else if
    else if(url.contains("tax"))
    {
        taxDAO = ParsedTaxData.getTaxInfo(jsonObject);
        contents = taxDAO.getTaxInfo();
        showWebView(contents);
    }//else if
    else if(url.contains("logistics"))
    {
        logisticsDAO = ParsedLogisticsData.getLogisticsInfo(jsonObject);
        contents = logisticsDAO.getLogisticsInfo();
        showWebView(contents);
    }//else if
    else if(url.contains("legal"))
    {
        legalDAO = ParsedLegalInfo.getLegalInfo(jsonObject);
        contents = legalDAO.getLegal_Info();
        showWebView(contents);
    }//else if
    else if(url.contains("smart_app"))
    {
        smartAppDAO = ParsedAppData.getAppInfo(jsonObject);
        contents = smartAppDAO.getAppInfo();
        showWebView(contents);
    }//else if
    pd.dismiss();
    Log.v(TAG, "onPostExecute called");
}//onPostExecute

private void showWebView(String contents) 
{
    if ((ctx.getResources().getConfiguration().screenLayout & 
            Configuration.SCREENLAYOUT_SIZE_MASK) == 1)
    {     
        contents = "<html><head>"
                  + "<style type=\"text/css\">body{color: #A17339;" +
                  "font-size:10px;" +
                  "text-align:justify;}"
                  + "</style></head>"
                  + "<body>"                          
                  + contents
                  + "</body></html>";
            Log.v(TAG+" onPostExecute if ldpi", "font setting for ldpi");
    }
    else if ((ctx.getResources().getConfiguration().screenLayout & 
            Configuration.SCREENLAYOUT_SIZE_MASK) == 2) 
    {     
        contents = "<html><head>"
              + "<style type=\"text/css\">body{color: #A17339;" +
              "font-size:12px;" +
              "text-align:justify;}"
              + "</style></head>"
              + "<body>"                          
              + contents
              + "</body></html>";
        Log.v(TAG+" onPostExecute if mdpi", "font setting for mdpi");
    } 
    else if ((ctx.getResources().getConfiguration().screenLayout & 
            Configuration.SCREENLAYOUT_SIZE_MASK) == 3)
    {     
        contents = "<html><head>"
              + "<style type=\"text/css\">body{color: #A17339;" +
              "font-size:18px;" +
              "text-align:justify;}"
              + "</style></head>"
              + "<body>"                          
              + contents
              + "</body></html>";
        Log.v(TAG+" onPostExecute if hdpi", "font setting for hdpi");
    }
    else if ((ctx.getResources().getConfiguration().screenLayout & 
            Configuration.SCREENLAYOUT_SIZE_MASK) == 4) {     
        contents = "<html><head>"
              + "<style type=\"text/css\">body{color: #A17339;" +
              "font-size:28px;" +
              "text-align:justify;}"
              + "</style></head>"
              + "<body>"                          
              + contents
              + "</body></html>";
        Log.v(TAG+" onPostExecute if xhdpi", "font setting for xhdpi");
    }
    //tieInfoObj.setInfo(contents);
    webInfo.loadData(contents, "text/html", "utf-8");
}//showWebView
}//TieInfoAsyncTask

1 个答案:

答案 0 :(得分:0)

new InfoAsyncTask(getParent(), infoUrl, webTieInfo).execute();

getParent()有点暴躁,只有在父级内Activity 嵌入时才有效。因此,在这种情况下,它似乎会产生null

请尝试以下方法,因为您要在AsyncTask内创建此Activity,这也是Context

new InfoAsyncTask(this, infoUrl, webTieInfo).execute();