在android中为不同屏幕大小实现不同布局时找不到视图id
我对碎片一无所知。我有一个实现片段的应用程序,它有不同的布局。当应用程序在智能手机和平板电脑中纵向运行时,它只显示一个布局(带有一个片段),而在平板电脑中横向运行时,它显示两个窗格布局(带有两个片段) 尝试在片段布局中获取视图id时出错。下面是res/layout和res/layout large中的activity_main_screen.xml代码在android中为不同屏幕大小实现不同布局时找不到视图id,android,android-layout,android-fragments,findviewbyid,Android,Android Layout,Android Fragments,Findviewbyid,我对碎片一无所知。我有一个实现片段的应用程序,它有不同的布局。当应用程序在智能手机和平板电脑中纵向运行时,它只显示一个布局(带有一个片段),而在平板电脑中横向运行时,它显示两个窗格布局(带有两个片段) 尝试在片段布局中获取视图id时出错。下面是res/layout和res/layout large中的activity_main_screen.xml代码 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/andro
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<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" />
</LinearLayout>
这是res/layout large land中的activity_main_screen.xml代码
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@color/grey">
<fragment android:name="com.pongodev.dr.know.QuestionFragment"
android:id="@+id/question_fragment"
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_marginRight="2dp"/>
<fragment android:name="com.pongodev.dr.know.AnswerFragment"
android:id="@+id/answer_fragment"
android:layout_weight="2"
android:layout_width="0dp"
android:layout_height="match_parent" />
</LinearLayout>
这是片段视图question_view.xml,包含按钮、edittext和listview
<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"
tools:context=".MainScreen"
android:background="@drawable/app_background">
<LinearLayout
android:id="@+id/lytSearchBar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/green"
android:orientation="horizontal"
android:layout_alignParentTop="true"
android:gravity="center_vertical"
android:padding="5dp">
<EditText
android:id="@+id/edtSearch"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="2"
android:textSize="14sp"
android:hint="@string/type_question"
android:textColor="@android:color/widget_edittext_dark"
android:layout_marginRight="5dp"
android:background="@drawable/edittext_style"
android:padding="12dp"
android:singleLine="true"/>
<ImageButton
android:id="@+id/btnSearch"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@android:drawable/ic_menu_search"
android:background="@drawable/button_style"
android:padding="5dp"/>
</LinearLayout>
<ListView
android:id="@id/android:list"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:fadeScrollbars="true"
android:fastScrollEnabled="true"
android:divider="@android:color/transparent"
android:dividerHeight="3dp"
android:layout_below="@+id/lytSearchBar"/>
</RelativeLayout>
这是QuestionFragment.java代码
public class QuestionFragment extends ListFragment {
OnQuestionSelectedListener mCallback;
ImageButton btnSearch;
QuestionListAdapter qla;
public interface OnQuestionSelectedListener{
public void onQuestionSelected(int position);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View v = inflater.inflate(R.layout.question_view, container, false);
//list = (ListView) v.findViewById(R.id.list);
prgLoading = (ProgressBar) v.findViewById(R.id.prgLoading);
txtAlert = (TextView) v.findViewById(R.id.txtAlert);
btnSearch = (ImageButton) v.findViewById(R.id.btnSearch);
btnSearch.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Toast.makeText(getActivity(), "button click", Toast.LENGTH_SHORT).show();
}
});
return v;
}
@Override
public void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
qla = new QuestionListAdapter(getActivity());
new getQuestionList().execute();
//setListAdapter(qla);
}
@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.answer_fragment) != null) {
getListView().setChoiceMode(ListView.CHOICE_MODE_SINGLE);
}
}
@Override
public void onAttach(Activity activity) {
super.onAttach(activity);
// This makes sure that the container activity has implemented
// the callback interface. If not, it throws an exception.
try {
mCallback = (OnQuestionSelectedListener) activity;
} catch (ClassCastException e) {
throw new ClassCastException(activity.toString()
+ " must implement OnHeadlineSelectedListener");
}
}
@Override
public void onListItemClick(ListView l, View v, int position, long id) {
// Notify the parent activity of selected item
mCallback.onQuestionSelected(position);
// Set the item as checked to be highlighted when in two-pane layout
getListView().setItemChecked(position, true);
}
public class getQuestionList extends AsyncTask<Void, Void, Void>{
getQuestionList(){
if(!prgLoading.isShown()){
prgLoading.setVisibility(0);
txtAlert.setVisibility(8);
}
}
@Override
protected void onPreExecute() {
// TODO Auto-generated method stub
}
@Override
protected Void doInBackground(Void... arg0) {
// TODO Auto-generated method stub
//parseJSONData();
return null;
}
@Override
protected void onPostExecute(Void result) {
// TODO Auto-generated method stub
prgLoading.setVisibility(8);
if((Question.length > 0)){
setListAdapter(qla);
}else{
txtAlert.setText(R.string.no_connection_alert);
txtAlert.setVisibility(0);
}
}
}
}
公共类QuestionFragment扩展了ListFragment{
OnQuestionSelectedListener McCallback;
图像按钮搜索;
qla问题;
问题SelectedListener的公共接口{
已选择公共职位(内部职位);
}
@凌驾
创建视图上的公共视图(布局、充气机、视图组容器、,
Bundle savedInstanceState){
//为该碎片膨胀布局
视图v=充气机。充气(右布局问题视图,容器,错误);
//list=(ListView)v.findViewById(R.id.list);
prgLoading=(ProgressBar)v.findViewById(R.id.prgLoading);
txtAlert=(TextView)v.findViewById(R.id.txtAlert);
btnSearch=(ImageButton)v.findviewbyd(R.id.btnSearch);
setOnClickListener(新的OnClickListener(){
@凌驾
公共void onClick(视图v){
//TODO自动生成的方法存根
Toast.makeText(getActivity(),“button click”,Toast.LENGTH_SHORT.show();
}
});
返回v;
}
@凌驾
创建时的公共void(Bundle savedInstanceState){
//TODO自动生成的方法存根
super.onCreate(savedInstanceState);
qla=新问题列表适配器(getActivity());
新建getQuestionList().execute();
//setListAdapter(qla);
}
@凌驾
public void onStart(){
super.onStart();
//在双窗格布局中,将listview设置为高亮显示选定的列表项
//(我们在onStart期间执行此操作,因为此时listview可用。)
if(getFragmentManager().findFragmentById(R.id.answer\u fragment)!=null){
getListView().setChoiceMode(ListView.CHOICE\u MODE\u SINGLE);
}
}
@凌驾
公共事务主任(活动){
超级转速计(活动);
//这确保容器活动已实现
//回调接口。如果不是,则抛出异常。
试一试{
mCallback=(OnQuestionSelectedListener)活动;
}catch(ClassCastException e){
抛出新的ClassCastException(activity.toString()
+“必须实现OnHeadlineSelectedListener”);
}
}
@凌驾
public void onListItemClick(列表视图l、视图v、整数位置、长id){
//通知所选项目的父活动
mCallback.onQuestionSelected(位置);
//将项目设置为选中状态,以便在双窗格布局中高亮显示
getListView().setItemChecked(位置,true);
}
公共类getQuestionList扩展了异步任务{
getQuestionList(){
如果(!prgLoading.isShown()){
prgLoading.setVisibility(0);
txtAlert.setVisibility(8);
}
}
@凌驾
受保护的void onPreExecute(){
//TODO自动生成的方法存根
}
@凌驾
受保护的Void doInBackground(Void…arg0){
//TODO自动生成的方法存根
//parseJSONData();
返回null;
}
@凌驾
受保护的void onPostExecute(void结果){
//TODO自动生成的方法存根
设置可见性(8);
如果((问题长度>0)){
setListAdapter(qla);
}否则{
setText(R.string.no_connection_alert);
txtAlert.setVisibility(0);
}
}
}
}
当我尝试获取应用程序中的按钮id时,出现get错误。如何修复此错误
更新:我不知道为什么,突然我的按钮id问题解决了。但是,ProgressBar id和TextView id还有其他问题
01-08 21:53:34.942: E/AndroidRuntime(23579): FATAL EXCEPTION: main
01-08 21:53:34.942: E/AndroidRuntime(23579): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.pongodev.dr.know/com.pongodev.dr.know.MainScreen}: java.lang.NullPointerException
01-08 21:53:34.942: E/AndroidRuntime(23579): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1970)
01-08 21:53:34.942: E/AndroidRuntime(23579): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1995)
01-08 21:53:34.942: E/AndroidRuntime(23579): at android.app.ActivityThread.access$600(ActivityThread.java:128)
01-08 21:53:34.942: E/AndroidRuntime(23579): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1161)
01-08 21:53:34.942: E/AndroidRuntime(23579): at android.os.Handler.dispatchMessage(Handler.java:99)
01-08 21:53:34.942: E/AndroidRuntime(23579): at android.os.Looper.loop(Looper.java:137)
01-08 21:53:34.942: E/AndroidRuntime(23579): at android.app.ActivityThread.main(ActivityThread.java:4514)
01-08 21:53:34.942: E/AndroidRuntime(23579): at java.lang.reflect.Method.invokeNative(Native Method)
01-08 21:53:34.942: E/AndroidRuntime(23579): at java.lang.reflect.Method.invoke(Method.java:511)
01-08 21:53:34.942: E/AndroidRuntime(23579): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:790)
01-08 21:53:34.942: E/AndroidRuntime(23579): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:557)
01-08 21:53:34.942: E/AndroidRuntime(23579): at dalvik.system.NativeStart.main(Native Method)
01-08 21:53:34.942: E/AndroidRuntime(23579): Caused by: java.lang.NullPointerException
01-08 21:53:34.942: E/AndroidRuntime(23579): at com.pongodev.dr.know.QuestionFragment$getQuestionList.<init>(QuestionFragment.java:140)
01-08 21:53:34.942: E/AndroidRuntime(23579): at com.pongodev.dr.know.QuestionFragment.onCreate(QuestionFragment.java:97)
01-08 21:53:34.942: E/AndroidRuntime(23579): at android.support.v4.app.Fragment.performCreate(Fragment.java:1437)
01-08 21:53:34.942: E/AndroidRuntime(23579): at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:877)
01-08 21:53:34.942: E/AndroidRuntime(23579): at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1088)
01-08 21:53:34.942: E/AndroidRuntime(23579): at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:682)
01-08 21:53:34.942: E/AndroidRuntime(23579): at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1444)
01-08 21:53:34.942: E/AndroidRuntime(23579): at android.support.v4.app.FragmentActivity.onStart(FragmentActivity.java:551)
01-08 21:53:34.942: E/AndroidRuntime(23579): at android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1137)
01-08 21:53:34.942: E/AndroidRuntime(23579): at android.app.Activity.performStart(Activity.java:4475)
01-08 21:53:34.942: E/AndroidRuntime(23579): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1943)
01-08 21:53:34.942: E/AndroidRuntime(23579): ... 11 more
01-08 21:53:34.942:E/AndroidRuntime(23579):致命异常:main
01-08 21:53:34.942:E/AndroidRuntime(23579):java.lang.RuntimeException:无法启动活动组件信息{com.pongodev.dr.know/com.pongodev.dr.know.MainScreen}:java.lang.NullPointerException
01-08 21:53:34.942:E/AndroidRuntime(23579):在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1970)
01-08 21:53:34.942:E/AndroidRuntime(23579):在android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1995)
01-08 21:53:34.942:E/AndroidRuntime(23579):在android.app.ActivityThread.access$600(ActivityThread.java:128)
01-08 21:53:34.942:E/AndroidRuntime(23579):位于android.app.ActivityThread$H.handleMessage(ActivityThread.java:1161)
01-08 21:53:34.942:E/AndroidRuntime(23579):在android.os.Handler.dispatchMessage(Handler.java:99)上
01-08 21:53:34.942:E/AndroidRuntime(23579):在android.os.Looper.loop(Looper.java:137)
01-08 21:53:34.942:E/AndroidRuntime(23579):位于android.app.ActivityThread.main(ActivityThread.java:4514)
01-08 21:53:34.942:E/AndroidRuntime(23579):位于java.lang.reflect.Method.Invokenactive(本机方法)
01-08 21:53:34.942:E/AndroidRuntime(23579):位于java.lang.reflect.Method.invoke(Method.java:511)
01-08 21:53:34.942:E/AndroidRuntime(23579):位于com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:790)
01-08 21:53:34.942:E/AndroidRuntime(23579):位于com.android.internal.os.ZygoteInit.main(ZygoteInit.java:557)
01-08 21:53:34.942:E/AndroidRuntime(23579):在dalvik.system.NativeStart.main(本机方法)
01-08 21:53:34.942:E/AndroidRuntime(23579):由以下原因引起:java.lang.NullPointerException
01-08 21:53:34.942: