我想構建具有不同佈局的應用程序以適應不同的操作模式。所以我建立了我的項目同樣做到了android教程[example here]。但有一點不同,當我想要用於各種方向模式(橫向,縱向等)時,本教程創建了不同的大小(正常和大)。現在我在取向變化期間獲得NullPointerException
。 這裏是我的代碼:定向更改期間出現NullPointerException
HeadlinesFragment:
public class HeadlinesFragment extends ListFragment {
OnHeadlineSelectedListener HeadlinesCallback;
List<Person> personLista = new ArrayList<Person>();
public interface OnHeadlineSelectedListener {
public void onArticleSelected(Person person);
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
int layout = Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB ?
android.R.layout.simple_list_item_activated_1 : android.R.layout.simple_list_item_1;
//TESTOWE WYPENIENIE
for(int i=0; i<10; i++){
personLista.add(new Person("Imie"+i+"Nazwisko"+i,"email"+i+"@example"+i+".com"));
}
String[] niceTime = new String[personLista.size()];
for(int i=0; i<personLista.size(); i++){
niceTime[i] = personLista.get(i).getName();
}
//KONIEC
setListAdapter(new ArrayAdapter<String>(getActivity(), layout, niceTime));
}
@Override
public void onStart() {
super.onStart();
// When in two-pane layout, set the listview to highlight the selected list item
// (We do this during onStart because at the point the listview is available.)
if (getFragmentManager().findFragmentById(R.id.person_view) != null) {
getListView().setChoiceMode(ListView.CHOICE_MODE_SINGLE);
}
}
@Override
public void onAttach(Activity activity){
super.onAttach(activity);
try{
HeadlinesCallback = (OnHeadlineSelectedListener)activity;
}catch(ClassCastException e){
throw new ClassCastException(activity.toString()
+ " have to implement OnHeadlineSelectedListener");
}
}
@Override
public void onListItemClick(ListView l, View v, int position, long id){
HeadlinesCallback.onArticleSelected(personLista.get(position));
getListView().setItemChecked(position, true);
}
}
PersonFragment:
public class PersonFragment extends Fragment{
public static Person person;
ImageView icon;
TextView text1;
TextView text2;
TextView text3;
View view;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
if(savedInstanceState != null){
person = (Person)savedInstanceState.getSerializable("person");
}
view = inflater.inflate(R.layout.person_view, container, false);
icon = (ImageView)view.findViewById(R.id.icon);
text1 = (TextView)view.findViewById(R.id.name);
text2 = (TextView)view.findViewById(R.id.email);
text3 = (TextView)view.findViewById(R.id.info);
return view;
}
@Override
public void onStart() {
super.onStart();
Bundle args = getArguments();
if (args != null) {
person = (Person)args.getSerializable("context");
text1.setText(person.getName());
text2.setText(person.getEmail());
text3.setText("Quisque semper ultricies iaculis. Vestibulum nec metus adipiscing, egestas sem mattis, ornare nulla. Quisque nec nibh nec lacus euismod gravida. Proin at pretium enim. In auctor, nisi sit amet pharetra facilisis, est justo mattis mi, eget mattis tortor ligula non orci. Vestibulum neque augue, elementum vitae aliquam non, condimentum ac dui. Nullam ultricies velit nec diam aliquam, sed lacinia sem eleifend. Quisque quam sapien, sodales id suscipit nec, consequat vel nunc. Quisque eu velit nisi. Donec varius consequat metus, tempor semper tortor ultricies vel. Mauris eget arcu nisi. Proin sit amet dui nisl. Quisque auctor lobortis augue sit amet egestas. Fusce mattis aliquam ante, et bibendum nibh ullamcorper et. Integer ipsum nibh, placerat a felis ut, accumsan rutrum eros. Quisque lacinia facilisis ipsum sed commodo.");
icon.setImageResource(R.drawable.ic_launcher);
}else{
if(person==null){
person = new Person("Test Testowy", "[email protected]");
}
updateArticleView(person);
}
}
@Override
public void onSaveInstanceState(Bundle outState){
super.onSaveInstanceState(outState);
outState.putSerializable("person", person);
}
public void updateArticleView(Person externalPerson) {
this.person = externalPerson;
text1.setText(externalPerson.getName());
text2.setText(externalPerson.getEmail());
text3.setText("Quisque semper ultricies iaculis. Vestibulum nec metus adipiscing, egestas sem mattis, ornare nulla. Quisque nec nibh nec lacus euismod gravida. Proin at pretium enim. In auctor, nisi sit amet pharetra facilisis, est justo mattis mi, eget mattis tortor ligula non orci. Vestibulum neque augue, elementum vitae aliquam non, condimentum ac dui. Nullam ultricies velit nec diam aliquam, sed lacinia sem eleifend. Quisque quam sapien, sodales id suscipit nec, consequat vel nunc. Quisque eu velit nisi. Donec varius consequat metus, tempor semper tortor ultricies vel. Mauris eget arcu nisi. Proin sit amet dui nisl. Quisque auctor lobortis augue sit amet egestas. Fusce mattis aliquam ante, et bibendum nibh ullamcorper et. Integer ipsum nibh, placerat a felis ut, accumsan rutrum eros. Quisque lacinia facilisis ipsum sed commodo.");
icon.setImageResource(R.drawable.ic_launcher);
}
}
和MainActivity:
public class MainActivity extends FragmentActivity implements OnHeadlineSelectedListener {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if(findViewById(R.id.fragment_container)!=null){
if (savedInstanceState != null) {
return;
}
getSupportFragmentManager().beginTransaction()
.add(R.id.fragment_container, new HeadlinesFragment()).commit();
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public void onArticleSelected(Person person) {
PersonFragment fragment = (PersonFragment)getSupportFragmentManager().findFragmentById(R.id.person_view);
if(fragment!=null){
fragment.updateArticleView(person);
}else{
PersonFragment newFragment = new PersonFragment();
Bundle args = new Bundle();
args.putSerializable("context", person);
newFragment.setArguments(args);
FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
transaction.replace(R.id.fragment_container, newFragment);
transaction.addToBackStack(null);
transaction.commit();
}
}
}
問題: 在開始一切正常,但是當我改變方向模式爲橫向(在模擬器CTRL + F12)和我再次回到portain,我從列表視圖中選擇一些東西,我碰到一個「遺憾的是應用程序已停止「。
這是logcat的:
11-02 14:29:03.396: E/AndroidRuntime(802): FATAL EXCEPTION: main
11-02 14:29:03.396: E/AndroidRuntime(802): java.lang.NullPointerException
11-02 14:29:03.396: E/AndroidRuntime(802): at com.example.friendslist.PersonFragment.updateArticleView(PersonFragment.java:68)
11-02 14:29:03.396: E/AndroidRuntime(802): at com.example.friendslist.MainActivity.onArticleSelected(MainActivity.java:43)
11-02 14:29:03.396: E/AndroidRuntime(802): at com.example.friendslist.HeadlinesFragment.onListItemClick(HeadlinesFragment.java:69)
11-02 14:29:03.396: E/AndroidRuntime(802): at android.support.v4.app.ListFragment$2.onItemClick(ListFragment.java:58)
11-02 14:29:03.396: E/AndroidRuntime(802): at android.widget.AdapterView.performItemClick(AdapterView.java:298)
11-02 14:29:03.396: E/AndroidRuntime(802): at android.widget.AbsListView.performItemClick(AbsListView.java:1100)
11-02 14:29:03.396: E/AndroidRuntime(802): at android.widget.AbsListView$PerformClick.run(AbsListView.java:2749)
11-02 14:29:03.396: E/AndroidRuntime(802): at android.widget.AbsListView$1.run(AbsListView.java:3423)
11-02 14:29:03.396: E/AndroidRuntime(802): at android.os.Handler.handleCallback(Handler.java:725)
11-02 14:29:03.396: E/AndroidRuntime(802): at android.os.Handler.dispatchMessage(Handler.java:92)
11-02 14:29:03.396: E/AndroidRuntime(802): at android.os.Looper.loop(Looper.java:137)
11-02 14:29:03.396: E/AndroidRuntime(802): at android.app.ActivityThread.main(ActivityThread.java:5041)
11-02 14:29:03.396: E/AndroidRuntime(802): at java.lang.reflect.Method.invokeNative(Native Method)
11-02 14:29:03.396: E/AndroidRuntime(802): at java.lang.reflect.Method.invoke(Method.java:511)
11-02 14:29:03.396: E/AndroidRuntime(802): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
11-02 14:29:03.396: E/AndroidRuntime(802): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
11-02 14:29:03.396: E/AndroidRuntime(802): at dalvik.system.NativeStart.main(Native Method)
我的主要問題是,我一直在尋找的東西,如「致」,在這裏,我沒有了吧。有人能告訴我問題在哪裏嗎?
P.S在普通的android例子中,當我從layout-large更改名稱的子文件夾到layout-land時,我有同樣的問題。
活動:
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/fragment_container"
android:layout_width="match_parent"
android:layout_height="match_parent" />
person_view:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:layout_marginRight="10dp"
android:layout_marginLeft="10dp" >
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">
<ImageView
android:id="@+id/icon"
android:layout_width="72dp"
android:layout_height="74dp"
android:src="@drawable/ic_launcher" />
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="match_parent" >
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:id="@+id/name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Imie i nazwisko" />
<TextView
android:id="@+id/email"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="[email protected]" />
</LinearLayout>
</LinearLayout>
</LinearLayout>
<TextView
android:id="@+id/info"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:text="info" />
在土地子目錄
主要活動:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent">
<fragment android:name="com.example.friendslist.HeadlinesFragment"
android:id="@+id/headlines_fragment"
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="match_parent" />
<fragment android:name="com.example.friendslist.PersonFragment"
android:id="@+id/person_view"
android:layout_weight="2"
android:layout_width="0dp"
android:layout_height="match_parent" />
什麼是線片段66? – Raghunandan
'java.lang.NullPointerException 11-02 14:06:14.696:E/AndroidRuntime(746):at com.example.friendslist.PersonFragment.updateArticleView(PersonFragment.java:66)'這是第一行 - 第66行Raghunandan說 – Simon
「icon =(ImageView)getActivity()。findViewById(R.id.icon);」我認爲它只是第一次感應findViewById method.P。S此行來自updateArticleView方法(PersonFragment中的最後一個方法)。我如何以其他方式獲得圖標指涉? – bkowalczyyk