如何将此类放入片段中。
问题无法解决方法findviewbyID
你可以帮我解决一个我需要替代方案的解决方案吗? 使这段代码工作这是我试图将其转换为片段的活动
public class MainActivity extends Fragment {
TextView fajrTV;
TextView zuhrTV;
TextView asrTV;
TextView maghribTV;
TextView ishaTV;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.activity_parse_salah_times, container, false);
new GetMethodEx().execute("http://muslimsalat.com/11717/daily/4.json?key=ea4417ebf152c478082e1ff6a1a759d9");
return rootView;}
public class GetMethodEx extends AsyncTask<String, Void, InputStream> {
@Override
protected void onPreExecute() {
super.onPreExecute();
fajrTV = (TextView) findViewById(R.id.fajr);
zuhrTV = (TextView) findViewById(R.id.zuhr);
asrTV = (TextView) findViewById(R.id.asr);
maghribTV = (TextView) findViewById(R.id.maghrib);
ishaTV = (TextView) findViewById(R.id.isha);
}
@Override
protected InputStream doInBackground(String... params) {
try {
HttpClient client = new DefaultHttpClient();
HttpPost post = new HttpPost(params[0]);
HttpResponse response = client.execute(post);
int status = response.getStatusLine().getStatusCode();
if (status == 200) {
HttpEntity entity = response.getEntity();
InputStream in = entity.getContent();
return in;
}
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(InputStream in) {
super.onPostExecute(in);
try {
JsonReader reader = new JsonReader(new InputStreamReader(in, "UTF-8"));
String fajr = null;
String zuhr = null;
String asr = null;
String maghrib = null;
String isha = null;
reader.beginObject();
while (reader.hasNext()) {
String name = reader.nextName();
if (name.equals("items")) {
reader.beginArray();
while (reader.hasNext()) {
reader.beginObject();
while (reader.hasNext()) {
String innerName = reader.nextName();
final boolean isInnerNull = reader.peek() == JsonToken.NULL;
if (innerName.equals("fajr") && !isInnerNull) {
fajr = reader.nextString();
} else if (innerName.equals("dhuhr") && !isInnerNull) {
zuhr = reader.nextString();
} else if (innerName.equals("asr") && !isInnerNull) {
asr = reader.nextString();
} else if (innerName.equals("maghrib") && !isInnerNull) {
maghrib = reader.nextString();
} else if (innerName.equals("isha") && !isInnerNull) {
isha = reader.nextString();
} else {
reader.skipValue();
}
}
reader.endObject();
}
reader.endArray();
} else {
reader.skipValue();
}
}
reader.endObject();
reader.close();
fajrTV.setText(fajr);
zuhrTV.setText(zuhr);
asrTV.setText(asr);
maghribTV.setText(maghrib);
ishaTV.setText(isha);
} catch (IOException e) {
e.printStackTrace();
}
}
}
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getActivity().getMenuInflater().inflate(R.menu.menu_main, 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();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
答案 0 :(得分:0)
Fragment
不包含这样的方法。如果您想要重现类似的功能,请保留对View
中返回的onCreateView
的引用,然后在该引用上调用findViewById
。
答案 1 :(得分:0)
试试这个
public class YourFragment extends Fragment{
View rootView; // create your rootView in here
@Override
public View onCreateView(LayoutInflater inflater,
ViewGroup container, Bundle savedInstanceState) {
...
rootView = inflater.inflate(R.layout.your_fragment, container, false);
return rootView;
}
....
public class GetMethodEx extends AsyncTask<String, Void, InputStream> {
...
@Override
protected void onPreExecute() {
super.onPreExecute();
fajrTV = (TextView) rootView.findViewById(R.id.fajr); // use the rootView
zuhrTV = (TextView) rootView.findViewById(R.id.zuhr);
asrTV = (TextView) rootView.findViewById(R.id.asr);
maghribTV = (TextView) rootView.findViewById(R.id.maghrib);
ishaTV = (TextView) rootView.findViewById(R.id.isha);
}
}
...
}
希望这个帮助