所以我遇到了这个问题。我知道为什么。但是第一个代码,所以你也会知道为什么:)。
LoveMailsActivity.java:
package com.example.ex;
import android.app.DialogFragment;
import android.app.FragmentManager;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;
public class LoveMailsActivity extends AppCompatActivity
{
FragmentManager fragmentManager;
CustomDialogFragment CustomDialogFragment;
//CustomArrayAdapter customArrayAdapter;
ArrayAdapter<String> arrayAdapter;
ListView dialogListView;
TextView loveEmailOpeningWordsHeaderTextView;
String[] items = {"Hello","Hello there","Hi","Hi there"};
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_love_mails);
viewsInitialization();
fragmentManager = getFragmentManager();
CustomDialogFragment = new CustomDialogFragment();
//customArrayAdapter = new CustomArrayAdapter(LoveMailsActivity.this, items);
arrayAdapter = new ArrayAdapter<String>(LoveMailsActivity.this, R.layout.list_view_row,R.id.row_text_view_id,items);
dialogListView.setAdapter(arrayAdapter); // NullPointerExeption
loveEmailOpeningWordsHeaderTextView.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View v)
{
CustomDialogFragment.show(fragmentManager, "Opening words");
}
});
} // End of onCreate() method
@Override
public boolean onCreateOptionsMenu(Menu menu)
{
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.love_mails, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item)
{
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings)
{
return true;
}
return super.onOptionsItemSelected(item);
}
private void viewsInitialization()
{
loveEmailOpeningWordsHeaderTextView = (TextView) findViewById(R.id.love_email_opening_words_header_text_view_id);
dialogListView = (ListView) findViewById(R.id.dialog_list_view_id);
}
public class CustomDialogFragment extends DialogFragment
{
TextView listViewItemTextView;
ArrayAdapter<String> arrayAdapter;
ListView dialogListView;
String[] items = {"Hello","Hello there","Hi","Hi there"};
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState)
{
View rootView = inflater.inflate(R.layout.dialog, container,false);
getDialog().setTitle("Choose an option"); // Set dialog title
listViewItemTextView = (TextView) rootView.findViewById(R.id.list_view_item_text_view_id);
dialogListView = (ListView) rootView.findViewById(R.id.dialog_list_view_id);
getDialog().setTitle("Opening Words"); // Setting dialog title
/*
CustomArrayAdapter customArrayAdapter = new CustomArrayAdapter(getActivity(), items);
dialogListView.setAdapter(customArrayAdapter);
*/
dialogListView.setOnItemClickListener(new OnItemClickListener()
{
@Override
public void onItemClick(AdapterView<?> parent, View view,int position, long id)
{
Toast.makeText(getActivity(), items[position], Toast.LENGTH_SHORT).show();
}
});
return rootView;
}
}
}
activity_love_mails.xml:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.predesignedmails.LoveMailsActivity" >
<TextView
android:id="@+id/love_email_emai_to_send_to_text_view_id"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/emai_to_send_to_text_view_text"
android:textSize="16sp"
android:textStyle="bold"
android:textColor="@color/opening_words_list_view_header_color"
/>
<EditText
android:id="@+id/love_email_email_to_send_to_edit_text_id"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:hint="@string/emai_to_send_to_edit_text_hint"
android:inputType="textEmailAddress"
android:textSize="18sp"
android:textColor="@color/selection_text_color"
android:layout_below="@id/love_email_emai_to_send_to_text_view_id"
/>
<TextView
android:id="@+id/love_email_opening_words_header_text_view_id"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/love_email_opening_words_text_view_text"
android:textSize="16sp"
android:textStyle="bold"
android:textColor="@color/opening_words_list_view_header_color"
android:layout_below="@id/love_email_email_to_send_to_edit_text_id"
/>
<TextView
android:id="@+id/love_email_opening_words_text_view_id"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/initial_text"
android:textSize="18sp"
android:textColor="@color/selection_text_color"
android:layout_below="@id/love_email_opening_words_header_text_view_id"
/>
</RelativeLayout>
现在我在NullPointerException
行显示dialogListView.setAdapter(arrayAdapter); // NullPointerExeption
,因为我在activity_love_mails.xml中没有任何名为ListView
的{{1}}。我在另一个XML文件中有这种dialog_list_view_id
:
dialog.xml:
ListView
现在我做错了什么?我实现这一点的方式显然是错误的,无法弄清楚是什么? <?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<ListView
android:id="@+id/dialog_list_view_id"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
</RelativeLayout>
班? CustomDialogFragment
?
修改
logcat的:
ArrayAdapter
答案 0 :(得分:1)
仅仅创建片段的对象并不意味着它会在onCreateView()或任何其他生命周期方法被调用。 您可以在活动中处理片段,也可以在活动XML中声明列表视图。 然后只有你可以获得listview。 对于交易片段你搜索谷歌你会有很多例子。
现在在你的情况下它是对话片段,所以这可以使用show()来完成。
此外,当片段不是对话片段时,活动中的findviewbyId()有效,因为片段视图成为活动视图层次结构的一部分。
答案 1 :(得分:0)
您的listview位于对话框片段中,并且您尝试在活动中调用findViewById,因此无法找到它,因此无效。
将适配器设置为onCreateView方法中对话框片段中的列表视图。
干杯:)