0

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

快速行动的代码

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
            }           
        });

添加标签规范的代码

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
4

1 回答 1

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

getParent()有点暴躁,只有当您Activity嵌入父母时才有效。因此,在这种情况下,它似乎会导致null.

请尝试以下操作,因为您是AsyncTask从一个 中创建的Activity,它也是一个Context

new InfoAsyncTask(this, infoUrl, webTieInfo).execute();
于 2012-10-31T05:34:13.720 回答