我正在尝试创建一个标题不同的ActionBar。这取决于我的ListView中单击了哪一个。它给了我一个Nullpointerexception。
public class SuggestFrag extends Fragment implements View.OnClickListener {
Button suggestButton;
TextView title;
EditText suggestion;
Annonce annonce;
@Override
public View onCreateView(LayoutInflater i, ViewGroup container, Bundle savedInstanceState) {
View rod = i.inflate(R.layout.frag_suggest, container, false);
setHasOptionsMenu(true);
getActivity().getActionBar().setTitle(annonce.getItemname()); //the errors occurs here
suggestButton = (Button)rod.findViewById(R.id.suggestknap);
suggestButton.setOnClickListener(this);
suggestion = (EditText)rod.findViewById(R.id.suggestion);
annonce = ((AnnonceDisplay)this.getActivity()).getAnnonce();
return rod;
}
@Override
public void onClick(View view) {
if(view == suggestButton) {
AlertDialog alert = new AlertDialog.Builder(getActivity())
.setMessage(suggestion.getText().toString())
.setPositiveButton("Send", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
launchintent();
}
}
)
.setNegativeButton("Ændre", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
}
}
).show();
}
}
private void launchintent() {
FragmentManager fm = getFragmentManager();
Fragment f = fm.findFragmentById(R.id.fragment);
if(fm.getBackStackEntryCount() > 0){
fm.popBackStack();
} else{
getActivity().finish();
}
}
@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
inflater.inflate(R.menu.menu_main, menu);
super.onCreateOptionsMenu(menu, inflater);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == R.id.Forside) {
Intent intent = new Intent(this.getActivity(), Forside.class);
startActivity(intent);
}
else if (id == R.id.Logud){
Intent intent = new Intent(this.getActivity(), LogInd.class);
startActivity(intent);
}
return true;
}
}
在我的活动中,它适用于汉堡包(或其他所谓的)。
编辑:
public class AnnonceDisplay extends Activity {
private Annonce annonce;
private Forslag forslag;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_annonce);
annonce = (Annonce)getIntent().getExtras().getSerializable("annonce");
if(savedInstanceState == null) {
Fragment fragment = new AnnonceFrag();
getFragmentManager().beginTransaction()
.add(R.id.fragment, fragment)
.commit();
}
}
public Annonce getAnnonce() {
return annonce;
}
public Forslag getForslag() { return forslag; }
public void setForlsag(Forslag forslag) {
this.forslag = forslag;
}
}
我在logcat中遇到的错误:
12-07 15:28:53.270 2056-2056/com.example.hadi.do2get E/AndroidRuntime: FATAL EXCEPTION: main
12-07 15:28:53.270 2056-2056/com.example.hadi.do2get E/AndroidRuntime: Process: com.example.hadi.do2get, PID: 2056
12-07 15:28:53.270 2056-2056/com.example.hadi.do2get E/AndroidRuntime: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.app.ActionBar.setTitle(java.lang.CharSequence)' on a null object reference
12-07 15:28:53.270 2056-2056/com.example.hadi.do2get E/AndroidRuntime: at com.example.hadi.do2get.SuggestFrag.onCreateView(SuggestFrag.java:35)
12-07 15:28:53.270 2056-2056/com.example.hadi.do2get E/AndroidRuntime: at android.app.Fragment.performCreateView(Fragment.java:2053)
12-07 15:28:53.270 2056-2056/com.example.hadi.do2get E/AndroidRuntime: at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:894)
12-07 15:28:53.270 2056-2056/com.example.hadi.do2get E/AndroidRuntime: at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1067)
12-07 15:28:53.270 2056-2056/com.example.hadi.do2get E/AndroidRuntime: at android.app.BackStackRecord.run(BackStackRecord.java:834)
12-07 15:28:53.270 2056-2056/com.example.hadi.do2get E/AndroidRuntime: at android.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1452)
12-07 15:28:53.270 2056-2056/com.example.hadi.do2get E/AndroidRuntime: at android.app.FragmentManagerImpl$1.run(FragmentManager.java:447)
12-07 15:28:53.270 2056-2056/com.example.hadi.do2get E/AndroidRuntime: at android.os.Handler.handleCallback(Handler.java:739)
12-07 15:28:53.270 2056-2056/com.example.hadi.do2get E/AndroidRuntime: at android.os.Handler.dispatchMessage(Handler.java:95)
12-07 15:28:53.270 2056-2056/com.example.hadi.do2get E/AndroidRuntime: at android.os.Looper.loop(Looper.java:135)
12-07 15:28:53.270 2056-2056/com.example.hadi.do2get E/AndroidRuntime: at android.app.ActivityThread.main(ActivityThread.java:5257)
12-07 15:28:53.270 2056-2056/com.example.hadi.do2get E/AndroidRuntime: at java.lang.reflect.Method.invoke(Native Method)
12-07 15:28:53.270 2056-2056/com.example.hadi.do2get E/AndroidRuntime: at java.lang.reflect.Method.invoke(Method.java:372)
12-07 15:28:53.270 2056-2056/com.example.hadi.do2get E/AndroidRuntime: at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
12-07 15:28:53.270 2056-2056/com.example.hadi.do2get E/AndroidRuntime: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
答案 0 :(得分:0)
当然它将为null,因为annonce未初始化。首先初始化annonce,然后设置标题,如下所示:
@Override
public View onCreateView(LayoutInflater i, ViewGroup container, Bundle savedInstanceState) {
View rod = i.inflate(R.layout.frag_suggest, container, false);
setHasOptionsMenu(true);
suggestButton = (Button)rod.findViewById(R.id.suggestknap);
suggestButton.setOnClickListener(this);
suggestion = (EditText)rod.findViewById(R.id.suggestion);
//// Initialize first
annonce = ((AnnonceDisplay)this.getActivity()).getAnnonce();
//// Now set title
getActivity().getActionBar().setTitle(annonce.getItemname());
return rod;
}
答案 1 :(得分:0)
将主题设置为清单文件中的活动
<activity android:name=".YourActivity"
android:theme="@android:style/Theme.Holo.Light.DarkActionBar"/>
如果你使用来自支持librery的主题调用getSupportActionBar();