首先:是的,我阅读了关于这个主题的所有其他主题.不仅是来自这个网站的人......(你看,我有点沮丧)
他们中的大多数都提供了使用建议,android:id而不仅仅是id在XML文件中.我做到了.
我从其他人那里了解到,View.findViewById与众不同Activity.findViewById.我也是这样处理的.
在我location_layout.xml,我使用:
<FrameLayout .... >
<some.package.MyCustomView ... />
<LinearLayout ... >
<TextView ...
android:id="@+id/txtLat" />
...
</LinearLayout>
</FrameLayout>
Run Code Online (Sandbox Code Playgroud)
在我的活动中,我做了:
...
setContentView( R.layout.location_layout );
Run Code Online (Sandbox Code Playgroud)
在我的自定义视图类中:
...
TextView tv = (TextView) findViewById( R.id.txtLat );
Run Code Online (Sandbox Code Playgroud)
返回null.这样做,我的活动工作正常.也许这是因为Activity.findViewById和View.findViewById差异.所以我在本地存储了传递给海关视图构造函数的上下文并尝试:
...
TextView tv = (TextView) ((Activity) context).findViewById( R.id.txtLat );
Run Code Online (Sandbox Code Playgroud)
这也回来了null.
然后,我改变了我的自定义视图扩展ViewGroup,而不是View和改变location_layout.xml让TextView我的自定义视图的直接子,从而使View.findViewById应该工作的设想.惊喜:它没有解决任何问题.
那么我做错了什么?
我会感激任何评论.
这两种方法之间的本质区别是什么?当我创建TextView时,我应该使用其中一个来提高性能吗?
编辑:有什么区别
onCreateView() {
root = some view
View v = new View(some context);
root.add(v);
return root;
}
onViewCreated() {
View v = new View(some context);
getView().add(v);
}
Run Code Online (Sandbox Code Playgroud) 我有一个FragmentActivity我添加的地方Fragment.根据某些情况,我想Buttons从该片段的布局访问并更改它们的单击侦听器.
我所做的是:
View view = myFragment.getView();
Button myButton = (Button) view.findViewById(R.id.my_button);
myButton.setOnClickListener(new MyClickListener());
Run Code Online (Sandbox Code Playgroud)
但view变量总是如此null.
我如何获得活动的视图?
有没有办法在片段范围内通过id查找视图?我正在使用一系列片段来呈现专门的列表.片段从布局加载,因此它们的小部件都具有相同的ID.
我想我可以找到一种方法,在创建期间(或之后)为每个小部件提供自定义ID.但是,如果我能以某种方式将findViewById限制在片段的范围内,那将会更好.
嗨,我为我的视频网站创建了一个webview应用程序.该网站的设计是为移动用户加载的混合设备.只有与移动设备兼容的视频才会加载到混合动力车上.球员来自Vk,dailymotion,youtube,quicktime.这些视频只能在sdk 11及更高版本上播放,但是当我点击播放器按钮进入全屏时,它只会停止播放视频,而不会启动进入全屏模式.我尽可能多地包含代码,希望有人可以帮助我.我没有取得任何进展,我用Google搜索了它.任何帮助将不胜感激.
(Webviewactivity.java)
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getWindow().requestFeature(Window.FEATURE_PROGRESS);
setContentView(R.layout.main);
parentView = (RelativeLayout) findViewById(R.id.parent_rl);
webviewProgress = (ProgressBar) findViewById(R.id.webview_progress);
webview = (WebView) findViewById(R.id.webview);
webview.getSettings().setJavaScriptEnabled(true);
webview.getSettings().setBuiltInZoomControls(true);
webview.getSettings().setAllowFileAccess(true);
webview.setWebViewClient(new MyWebViewClient());
webview.getSettings().setPluginState(WebSettings.PluginState.ON);
webview.loadUrl(URL);
webviewProgress.setProgress(0);
webview.setWebChromeClient(new MyWebChromeClient());
webview.setDownloadListener(new DownloadListener() {
public void onDownloadStart(String url, String userAgent,
String contentDisposition, String mimetype,
long contentLength) {
mProgressDialog = new ProgressDialog(WebViewActivity.this);
mProgressDialog.setMessage("Downloading...");
mProgressDialog.setIndeterminate(false);
mProgressDialog.setMax(100);
mProgressDialog
.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
DownloadFile downloadFile = new DownloadFile();
downloadFile.execute(url);
}
});
initSlider();
initAdmob();
}
/**
* When when file was chosen
*/
@Override
protected void …Run Code Online (Sandbox Code Playgroud) 我最近将项目从Eclipse移到了Android Studio.我试图让电子邮件功能正常工作,但我收到"findViewById"错误.我也收到"Toast.makeText"的错误.你能帮忙解决这两个错误.我班上的代码如下:
package com.example.ishonours.witsbusapp;
import android.app.Activity;
import android.net.Uri;
import android.os.Bundle;
import android.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.content.Intent;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class ComplaintsFragment extends Fragment {
//I added
private EditText recipient;
private EditText subject;
private EditText body;
private OnFragmentInteractionListener mListener;
private static final String ARG_SECTION_NUMBER = "5";
public static ComplaintsFragment newInstance(int menuNumber) {
ComplaintsFragment fragment = new ComplaintsFragment();
Bundle args = new Bundle();
args.putInt(ARG_SECTION_NUMBER, menuNumber);
fragment.setArguments(args);
return fragment;
}
public ComplaintsFragment() {
// Required …Run Code Online (Sandbox Code Playgroud) 我想ArrayAdapter在我的课程中实现一个Fragment不扩展的课程Activity.
问题是传递给ArrayAdapter的构造函数的第一个参数.
ArrayAdapter<String> myAdapter = new ArrayAdapter<String>(<what place here>,android.R.layout.simple_list_item1,anyarraylist);
Run Code Online (Sandbox Code Playgroud)
现在我应该怎么做第一个参数?我试图通过
getActivity().getApplicationContext()
Run Code Online (Sandbox Code Playgroud)
在第一个参数但代码崩溃.
请帮帮我.
我的logcat:
03-17 03:33:08.029: E/AndroidRuntime(596): FATAL EXCEPTION: main
03-17 03:33:08.029: E/AndroidRuntime(596): java.lang.NullPointerException
03-17 03:33:08.029: E/AndroidRuntime(596): at com.search.pages.SearchResults.onCreateView(SearchResults.java:92)
03-17 03:33:08.029: E/AndroidRuntime(596): at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:871)
03-17 03:33:08.029: E/AndroidRuntime(596): at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1083)
03-17 03:33:08.029: E/AndroidRuntime(596): at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:635)
03-17 03:33:08.029: E/AndroidRuntime(596): at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1431)
03-17 03:33:08.029: E/AndroidRuntime(596): at android.support.v4.app.FragmentManagerImpl$1.run(FragmentManager.java:420)
03-17 03:33:08.029: E/AndroidRuntime(596): at android.os.Handler.handleCallback(Handler.java:587)
03-17 03:33:08.029: E/AndroidRuntime(596): at android.os.Handler.dispatchMessage(Handler.java:92)
03-17 03:33:08.029: E/AndroidRuntime(596): at android.os.Looper.loop(Looper.java:123)
03-17 03:33:08.029: E/AndroidRuntime(596): at android.app.ActivityThread.main(ActivityThread.java:4627) …Run Code Online (Sandbox Code Playgroud) 基本上,我正在尝试创建一个具有图片库视图的应用程序。我使用网格视图进行了画廊视图。使用片段,而不是活动。但是,打开图库会话时,应用程序崩溃。这是代码:
XML文件(fragment_gallery.xml):
<FrameLayout 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:padding="5dp"
tools:context="com.example.tonymathew.eliteenglishclub.gallery">
<!-- TODO: Update blank fragment layout -->
<GridView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/gridView"
android:columnWidth="100dp"
android:stretchMode="columnWidth"
android:numColumns="auto_fit"
android:horizontalSpacing="5dp"
android:verticalSpacing="5dp"
android:layout_gravity="center" />
</FrameLayout>
Run Code Online (Sandbox Code Playgroud)
片段文件(gallery.java):
public class gallery extends Fragment {
GridView gridView;
public gallery() {
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_gallery, container, false);
gridView = (GridView) container.findViewById(R.id.gridView);
gridView.setAdapter(new ImageAdapter(this));
return view;
}
}
Run Code Online (Sandbox Code Playgroud)
适配器类(Image Adapter.java):
public class ImageAdapter extends BaseAdapter{
private Context context;
public Integer[] images …Run Code Online (Sandbox Code Playgroud) 当我尝试测试我的应用程序时遇到问题,我是 Android Studio 的初学者并试图将 reyclerview 放在片段中,但是当我编译时,我收到此消息:
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.support.v7.widget.RecyclerView.setLayoutManager(android.support.v7.widget.RecyclerView$LayoutManager)' on a null object reference
Run Code Online (Sandbox Code Playgroud)
这是导致原因的文件:
package e.user.popcorn;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.annotation.NonNull;
import android.support.design.widget.BottomNavigationView;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.MenuItem;
import android.widget.FrameLayout;
import android.widget.TextView;
import android.widget.Toolbar;
import java.util.ArrayList;
import java.util.List;
public class MainActivity extends AppCompatActivity {
private BottomNavigationView mBottomNavigation;
private FrameLayout mMainFrame;
private HomeFragment homeFragment;
private MoviesFragment moviesFragment;
private UsersFragment usersFragment;
private ChatFragment chatFragment; …Run Code Online (Sandbox Code Playgroud) 我正在尝试从片段 xml 中获取 id 所以这里是错误代码“无法解析 findViewById 方法”
有没有我可以findViewById在 Fragment 中使用的?
public class Slide_Teacher_Add extends Fragment implements View.OnClickListener {
private EditText teacher_id_number;
private EditText teacher_fullname;
private EditText teacher_lesson;
private EditText teacher_contact;
private Button btn_add_teacher;
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.activity_slide__teacher_add,container,false);
return v;
teacher_id_number = (EditText) findViewById(R.id.teacher_id_number);
teacher_fullname = (EditText) findViewById(R.id.teacher_fullname);
teacher_lesson = (EditText) findViewById(R.id.teacher_lesson);
teacher_contact = (EditText) findViewById(R.id.teacher_contact);
btn_add_teacher = (Button) findViewById(R.id.btn_add_teacher);
btn_add_teacher.setOnClickListener(this);
}
Run Code Online (Sandbox Code Playgroud) android ×10
findviewbyid ×3
android-view ×1
eclipse ×1
fragment ×1
java ×1
video-player ×1
webview ×1