0
我有一個活動,擴展FragmentActivity說MainActivity,它包含兩個片段說F1,F2。在F1片段中,調用AsyncTask並在MainActivity中實現接口,即FragmentActivity。在那個接口方法中,嘗試調用新活動時,會說使用下面的代碼擴展Activity的SecondActivity,但它會拋出異常。從FragmentActivity調用活動NullPointerException
Intent intent = new Intent(MainActivity.this, SecondActivity.class);
startActivity(intent);
請幫忙。
代碼 MainActivity.java
import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.widget.TextView;
// Import
public class MainActivity extends FragmentActivity implements
SavePaymentInterface, GetDiscountInterface{
private final String TAG = "MainActivity";
public static Context mContext;
public static AppData mAppData;
public static WebServiceFunctions Obj_WebServiceFunctions;
public static Common obj_Common;
//var init
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
init();
initView();
}
@SuppressLint("NewApi")
private void init() throws NotFoundException {
actionBar = getActionBar();
actionBar.setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);
actionBar.setCustomView(R.layout.actionbar_view);
actionBar.setBackgroundDrawable(getResources().getDrawable(
R.drawable.top_portrait));
mAppSectionsPagerAdapter = new AppSectionsPagerAdapter(getSupportFragmentManager());
mViewPager = (CustomViewPager) findViewById(R.id.pager);
mViewPager.setAdapter(mAppSectionsPagerAdapter);
mContext = this;
mViewPager.setOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() {
@Override
public void onPageSelected(int position) {
mTabHost.setCurrentTab(position);
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.home, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// TODO Auto-generated method stub
switch (item.getItemId()) {
case R.id.action_logout:
break;
case R.id.action_print:
showAlertDialog(mContext.getString(R.string.STR_TITLE_RECEIPT_PRINT),
mContext.getString(R.string.STR_MESSAGE_RECEIPT_PRINT));
break;
case R.id.action_more:
break;
default:
return super.onOptionsItemSelected(item);
}
return true;
}
private void initView() {
try {
setupTabHost();
setupTab(new TextView(this), getString(R.string.TAB_TICKET),
0);
setupTab(new TextView(this), getString(R.string.TAB_CASHIER), 1);
} catch (Exception e) {
LogFile.writeLOG(TAG, "@initView(): " + e.toString());
}
addTabListener();
}
public static class AppSectionsPagerAdapter extends FragmentPagerAdapter {
public AppSectionsPagerAdapter(FragmentManager fm) {
super(fm);
}
@Override
public Fragment getItem(int i) {
switch (i) {
case 0:
MainActivity m = new MainActivity();
return m.new TicketFragment();
default:
MainActivity m1 = new MainActivity();
return m1.new CashierFragment();
}
}
@Override
public int getCount() {
return 2;
}
@Override
public CharSequence getPageTitle(int position) {
return "Section " + (position + 1);
}
}
/**
* A fragment that launches other parts of the demo application.
*/
@SuppressLint("ValidFragment")
public class TicketFragment extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.lay_ticket, container, false);
//variable init
return rootView;
}
@Override
public void onPause() {
super.onPause();
}
@Override
public void onResume()
{
super.onResume();
}
}
@SuppressLint("ValidFragment")
public class CashierFragment extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.lay_cashier, container, false);
//variable init
rootView.findViewById(R.id.btnSearch)
.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
// some code
}
});
rootView.findViewById(R.id.btnValidations)
.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
try {
new AsyncGetDiscountList(mContext, MainActivity.this,
Obj_WebServiceFunctions).execute();
} catch (Exception e) {
LogFile.writeLOG(TAG,
": " + e.toString());
}
}
});
rootView.findViewById(R.id.btnPayment)
.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
try {
new AsyncSavePayment(mContext,
MainActivity.this,
Obj_WebServiceFunctions).execute();
} catch (Exception e) {
LogFile.writeLOG(TAG, ": "
+ e.toString());
}
}
});
return rootView;
}
@Override
public void onPause() {
super.onPause();
}
@Override
public void onResume() {
super.onResume();
}
}
public void showAlertDialog(String strTitle, String strMessage) {
try {
// some code
txtTitle.setText(strTitle);
txtMessage.setText(strMessage);
Button ok = (Button) mDialog.findViewById(R.id.btnYes);
ok.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
receiptPrint();
}
});
Button cancel = (Button) mDialog.findViewById(R.id.btnNo);
cancel.setText(mContext.getString(R.string.STR_LBL_NO));
cancel.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
mDialog.dismiss();
}
});
mDialog.show();
} catch (Exception e) {
LogFile.writeLOG(TAG, "@showAlertDialog(): " + e.toString());
}
}
private void receiptPrint() {
try {
Intent intent = new Intent(MainActivity.this, PrintReceiptActivity.class);
intent.putExtra("print", isPrint); // throws nullpointer exception
startActivity(intent);
} catch (Exception e) {
LogFile.writeLOG(TAG, "@receiptPrint(): " + e.toString());
}
}
@Override
public void onPaymentDone(String response) {
try {
if (response) { //condition for response validility
isPrint = true;
showAlertDialog(
mContext.getString(R.string.STR_TITLE_RECEIPT_PRINT),
mContext.getString(R.string.STR_MESSAGE_RECEIPT_PRINT));
}
} catch (Exception e) {
LogFile.writeLOG(TAG, "@onPaymentDone(): " + e.toString());
}
}
@Override
public Common getCommon() {
return obj_Common;
}
private void addTabListener() {
mTabHost.setOnTabChangedListener(new OnTabChangeListener() {
@Override
public void onTabChanged(String tabId) {
try {
if (mTabHost.getCurrentTab() == 0) {
// some code
} else if (mTabHost.getCurrentTab() == 1) {
// some code
}
} catch (Exception e) {
e.printStackTrace();
LogFile.writeLOG(TAG, "@addTabListener(): " + e.toString());
}
}
});
}
private void setupTabHost() {
mTabHost = (TabHost) findViewById(android.R.id.tabhost);
mTabHost.setup();
}
private void setupTab(final View view, final String tag, int index) {
try {
View tabview = createTabView(mTabHost.getContext(), tag, index);
TabSpec setContent = mTabHost.newTabSpec(tag).setIndicator(tabview).setContent(new TabContentFactory() {
@Override
public View createTabContent(String tag) {
return view;
}
});
mTabHost.addTab(setContent);
} catch (Exception e) {
LogFile.writeLOG(TAG, "@setupTab(): " + e.toString());
}
}
private View createTabView(final Context context, final String text,
int index) {
try {
View view = LayoutInflater.from(getApplicationContext()).inflate(
R.layout.lay_tabs_bg, null);
TextView tv = (TextView) view.findViewById(R.id.tabsText);
tv.setText(text);
if (index == 0) {
tv.setCompoundDrawablesWithIntrinsicBounds(R.drawable.ticket_icon, 0, 0, 0);
} else {
tv.setCompoundDrawablesWithIntrinsicBounds(R.drawable.cashier_icon, 0, 0, 0);
}
return view;
} catch (Exception e) {
LogFile.writeLOG(TAG, "@createTabView(): " + e.toString());
return null;
}
}
@Override
public void onBackPressed() {
}
@Override
public void onDestroy() {
super.onDestroy();
}
@Override
public void onPause() {
super.onPause();
}
@Override
public void onResume() {
}
@Override
public void onStart() {
super.onStart();
}
@Override
protected void onRestart() {
super.onRestart();
}
@Override
public void onGetDiscount(String response) {
try {
Intent intent = new Intent(MainActivity.this, SecondActivity.class); // throws nullpointer exception
startActivity(intent);
} catch (Exception e) {
}
}
}
的logcat:
09-30 17:52:21.143: E/AndroidRuntime(10600): FATAL EXCEPTION: main
09-30 17:52:21.143: E/AndroidRuntime(10600): java.lang.NullPointerException
09-30 17:52:21.143: E/AndroidRuntime(10600): at android.content.ContextWrapper.getPackageName(ContextWrapper.java:135)
09-30 17:52:21.143: E/AndroidRuntime(10600): at android.content.ComponentName.<init>(ComponentName.java:75)
09-30 17:52:21.143: E/AndroidRuntime(10600): at android.content.Intent.<init>(Intent.java:3678)
09-30 17:52:21.143: E/AndroidRuntime(10600): at com.vale.activity.MainActivity.receiptPrint(MainActivity.java:1093)
09-30 17:52:21.143: E/AndroidRuntime(10600): at com.vale.activity.activity.MainActivity.access$4(MainActivity.java:1075)
09-30 17:52:21.143: E/AndroidRuntime(10600): at com.vale.activity.activity.MainActivity$11.onClick(MainActivity.java:1048)
09-30 17:52:21.143: E/AndroidRuntime(10600): at android.view.View.performClick(View.java:4243)
09-30 17:52:21.143: E/AndroidRuntime(10600): at android.view.View$PerformClick.run(View.java:17731)
09-30 17:52:21.143: E/AndroidRuntime(10600): at android.os.Handler.handleCallback(Handler.java:730)
09-30 17:52:21.143: E/AndroidRuntime(10600): at android.os.Handler.dispatchMessage(Handler.java:92)
09-30 17:52:21.143: E/AndroidRuntime(10600): at android.os.Looper.loop(Looper.java:137)
09-30 17:52:21.143: E/AndroidRuntime(10600): at android.app.ActivityThread.main(ActivityThread.java:5136)
09-30 17:52:21.143: E/AndroidRuntime(10600): at java.lang.reflect.Method.invokeNative(Native Method)
09-30 17:52:21.143: E/AndroidRuntime(10600): at java.lang.reflect.Method.invoke(Method.java:525)
09-30 17:52:21.143: E/AndroidRuntime(10600): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
09-30 17:52:21.143: E/AndroidRuntime(10600): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
09-30 17:52:21.143: E/AndroidRuntime(10600): at dalvik.system.NativeStart.main(Native Method)
你能不能也分享你的日誌細節? – santalu 2014-09-30 12:11:32
發佈您的日誌詳細信息。 – Yugesh 2014-09-30 12:16:39
你可以發佈'AndroidManifest.xml'嗎? – PageNotFound 2014-09-30 12:26:32