我想访问TextView
以取消其标签。
但是,当代码位于 onCreate (), onResume ()或 onStart ()时,findViewById
似乎返回null。只有在
//if(Day != null)
没有被//.
评论将代码段放入onClickListener
的{{1}},而不是Button
时,效果很好。
我确实已经阅读了很多解决方案(清理项目,将代码移到NullPointerException
等),但它们都没有为我工作。
所以我真的希望得到你的帮助:P
我的活动是
onCreateView
}
XML-Code 的开头:
package edu.uahwi.smac;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.app.ActionBarActivity;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.TextView;
public class HoursActivity extends ActionBarActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//hides the title bar
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_hours);
final PlaceholderFragment fragment = new PlaceholderFragment();
Bundle args = new Bundle();
long ChosenDate = (long) getIntent().getExtras().getLong("DateLong");
args.putLong("DateLong", ChosenDate);
fragment.setArguments(args);
getSupportFragmentManager().beginTransaction().add(R.id.container, fragment).commit();
/*if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction()
.add(R.id.container, new PlaceholderFragment())
.commit();
}
*/
/*
TextView Day1 = (TextView) findViewById(R.id.textViewDay);
//if(Day != null)
Day1.setText(String.valueOf(ChosenDate));*/
}
public void onStart()
{
}
public void Hour2onClick(View v)
{
startActivity( new Intent("edu.uahwi.smac.BookingConfirmedActivity"));
}
public void buttonClick(View v)
{
//startActivity( new Intent("edu.uahwi.smac.BookingConfirmedActivity"));
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.hours, 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);
}
/**
* A placeholder fragment containing a simple view.
*/
public static class PlaceholderFragment extends Fragment {
public PlaceholderFragment() {
}
private long chosenDate; // = 10l;
//= (long) getIntent().getExtras().getLong("DateLong");
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_hours, container, false);
TextView Day1 = (TextView) rootView.findViewById(R.id.textViewDay);
Day1.setText(String.valueOf(chosenDate));
return rootView;
}
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
Bundle args = getArguments();
if (args != null)
chosenDate = args.getLong("DateLong");
}
}
最后我的 Stack-Trace :
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/LinearLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
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="edu.uahwi.smac.HoursActivity$PlaceholderFragment" >
<TextView
android:id="@+id/textViewDay1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:gravity="center"
android:paddingBottom="15dp"
android:text="@string/ChosenDay"
android:textSize="25sp" />
<ScrollView
android:id="@+id/scrollView1"
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="208dp"
android:orientation="vertical" >
答案 0 :(得分:0)
TextView在片段中。当您确定片段的视图存在时,您需要将设置文本值的代码移动到Fragment的onCreateView()上。像这样:
private long chosenDate;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
View rootView = inflater.inflate(R.layout.your_fragment_layout, container, false);
TextView Day1 = (TextView) findViewById(R.id.textViewDay1);
Day1.setText(String.valueOf(chosenDate));
并且您可以将selectedDate传递给片段参数,以便片段可以使用它。从Activity:
开始这样的片段final PlaceholderFragment fragment = new PlaceholderFragment();
Bundle args = new Bundle();
long ChosenDate = (long) getIntent().getExtras().getLong("DateLong");
args.putLong("DateLong", ChosenDate);
fragment.setArguments(args);
fm.beginTransaction().add(R.id.container, fragment).commit();
然后在片段的onCreate中,你可以提取参数并将本地私有long设置为该值,以便稍后在onViewCreate调用中可用:
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
Bundle args = getArguments();
if (args != null)
chosenDate = args.getLong("DateLong");