-2
我在導航抽屜的滑動xml中有textview,當我試圖訪問textview它給我在findviewId空指針錯誤任何人都可以告訴我如何訪問它?我試圖使用它,但我不能夠做這樣.....findViewById()在滑動導航抽屜中返回空值
here is my code:
public class AbstractActivity extends AppCompatActivity implements View.OnClickListener{
private String[] mPlanetTitles;
private LinearLayout mDrawerList;
protected DrawerLayout mDrawerLayout;
protected ActionBarDrawerToggle mDrawerToggle;
private CharSequence mDrawerTitle;
private CharSequence mTitle;
protected Toolbar toolbar;
protected FrameLayout mFrameLayout;
LinearLayout list0,list1,list2,list3,list4,list5,list6,list7,list8,list9,list10,list11,list12,list13,list14;
FancyButton btnAdd,btnChange;
private TextView txtUserName,txtUserDob,txtUserPlace;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_abstract);
mFrameLayout = (FrameLayout)findViewById(R.id.main_activity_content_frame);
txtUserName.findViewById(R.id.txtUserName);
txtUserDob.findViewById(R.id.txtUserDob);
txtUserPlace.findViewById(R.id.txtUserLocation);
SharedPreferences sharedPreferences = getSharedPreferences(Const.Shared.MY_PREFERNCES,MODE_PRIVATE);
String name = sharedPreferences.getString(Const.Shared.USER_NAME,null);
int day = sharedPreferences.getInt(Const.Shared.DAY,0);
int month = sharedPreferences.getInt(Const.Shared.MONTH,0);
int year = sharedPreferences.getInt(Const.Shared.YEAR,0);
String location = sharedPreferences.getString(Const.Shared.LOCATION,null);
txtUserName.setText(name);
txtUserDob.setText(day+"-"+month+"-"+year);
txtUserPlace.setText(location);
toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
mPlanetTitles = getResources().getStringArray(R.array.planets_array);
mDrawerLayout = (DrawerLayout) findViewById(R.id.main_activity_DrawerLayout);
//mDrawerList = (LinearLayout) findViewById(R.id.left_drawer);
mTitle = mDrawerTitle = getTitle().toString();
mDrawerLayout.closeDrawers();
mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout,
toolbar, R.string.drawer_open, R.string.drawer_close){
public void onDrawerClosed(View view) {
super.onDrawerClosed(view);
getSupportActionBar().setTitle(mTitle);
invalidateOptionsMenu(); // creates call to onPrepareOptionsMenu()
}
/** Called when a drawer has settled in a completely open state. */
public void onDrawerOpened(View drawerView) {
super.onDrawerOpened(drawerView);
getSupportActionBar().setTitle(mDrawerTitle);
invalidateOptionsMenu(); // creates call to onPrepareOptionsMenu()
}
@Override
public void onDrawerSlide(View drawerView, float slideOffset) {
// Disables the burger/arrow animation by default
super.onDrawerSlide(drawerView, 0);
}
};
if (getSupportActionBar() != null) {
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setDisplayShowHomeEnabled(true);
getSupportActionBar().setHomeButtonEnabled(true);
}
mDrawerToggle.syncState();
mDrawerLayout.setDrawerListener(mDrawerToggle);
//View frameNavigation = findViewById(R.id.fram)
list0 =(LinearLayout)findViewById(R.id.listm0);
list1 =(LinearLayout)findViewById(R.id.listm1);
list2 =(LinearLayout)findViewById(R.id.listm2);
list3 =(LinearLayout)findViewById(R.id.listm3);
list4 =(LinearLayout)findViewById(R.id.listm4);
list5 =(LinearLayout)findViewById(R.id.listm5);
list6 =(LinearLayout)findViewById(R.id.listm6);
list7 =(LinearLayout)findViewById(R.id.listm7);
list8 =(LinearLayout)findViewById(R.id.listm8);
list9 =(LinearLayout)findViewById(R.id.listm9);
list10 =(LinearLayout)findViewById(R.id.listm10);
list11 =(LinearLayout)findViewById(R.id.listm11);
list12 =(LinearLayout)findViewById(R.id.listm12);
list13 =(LinearLayout)findViewById(R.id.listm13);
list14 =(LinearLayout)findViewById(R.id.listm14);
btnAdd =(FancyButton)findViewById(R.id.btn_add);
btnChange =(FancyButton)findViewById(R.id.btn_change);
}
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu, menu);
return true;
}
public boolean onOptionsItemSelected(MenuItem item){
int items = item.getItemId();
switch(items){
case R.id.action_Contact_us:{
Intent intent = new Intent(this,Contact.class);
startActivity(intent);
}break;
case R.id.action_Aboutus:{
Intent intent = new Intent(this,AboutUs.class);
startActivity(intent);
}break;
case R.id.action_Profile:{
Intent intent = new Intent(this,HoroscopeChart.class);
startActivity(intent);
}break;
}
return super.onOptionsItemSelected(item);
}
@Override
public void onClick(View v) {
}
public void myclick(View v){
int id = v.getId();
switch (id) {
case R.id.btn_add: {
Intent intent = new Intent(this, Form.class);
startActivity(intent);
}break;
case R.id.btn_change: {
Intent intent = new Intent(this, Form.class);
startActivity(intent);
}break;
case R.id.listm0: {
Intent intent = new Intent(this, DailyPrediction.class);
startActivity(intent);
}
break;
case R.id.listm1: {
Intent intent = new Intent(this, AstroDetailsMain.class);
startActivity(intent);
}
break;
case R.id.listm2: {
Intent intent = new Intent(this, HoroscopeChart.class);
startActivity(intent);
}
break;
case R.id.listm3: {
Intent intent = new Intent(this, PlanetaryPositions.class);
startActivity(intent);
}
break;
case R.id.listm4: {
Intent intent = new Intent(this, VimshottariDashaMain.class);
startActivity(intent);
}
break;
case R.id.listm5: {
Intent intent = new Intent(this, YoginiDashaMain.class);
startActivity(intent);
}
break;
case R.id.listm6: {
Intent intent = new Intent(this, ChardashaMain.class);
startActivity(intent);
}
break;
case R.id.listm7: {
Intent intent = new Intent(this, AshtakVarga.class);
startActivity(intent);
}
break;
case R.id.listm8: {
Intent intent = new Intent(this, Kalsarp.class);
startActivity(intent);
}
break;
case R.id.listm9: {
Intent intent = new Intent(this, Manglik.class);
startActivity(intent);
}
break;
case R.id.listm10: {
Intent intent = new Intent(this, PitraDosha.class);
startActivity(intent);
}
break;
case R.id.listm11: {
Intent intent = new Intent(this, Sadhesati.class);
startActivity(intent);
}
break;
case R.id.listm12: {
Intent intent = new Intent(this, AscendantReport.class);
startActivity(intent);
}
break;
case R.id.listm13: {
Intent intent = new Intent(this, HouseReports.class);
startActivity(intent);
}
break;
case R.id.listm14: {
Intent intent = new Intent(this, SignReport.class);
startActivity(intent);
}
break;
}
}
}
<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/main_activity_DrawerLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true">
<!-- The main content view -->
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<include layout="@layout/kundli_toolbar" />
<FrameLayout
android:id="@+id/main_activity_content_frame"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
<!-- The navigation drawer -->
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:fancy="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="start"
android:fitsSystemWindows="true"
android:orientation="vertical"
>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@android:color/white"
android:clipToPadding="false"
android:paddingBottom="10dp"
android:paddingTop="0dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@android:color/white"
android:orientation="vertical"
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:fancy="http://schemas.android.com/apk/res-auto">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:background="@drawable/png"
>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:gravity="center"
>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:layout_marginBottom="10dp"
android:layout_marginTop="10dp">
<TextView
android:layout_marginTop="10dp"
android:layout_marginLeft="10dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/txtUserName"
android:textColor="@android:color/white"
android:textSize="22dp"/>
<TextView
android:layout_marginTop="10dp"
android:layout_marginLeft="10dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="@android:color/white"
android:id="@+id/txtUserDob"
android:textSize="16dp"/>
<TextView
android:layout_marginTop="10dp"
android:layout_marginLeft="10dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="@android:color/white"
android:id="@+id/txtUserLocation"
android:textSize="16dp"/>
</LinearLayout>
</LinearLayout>
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_marginTop="10dp">
<mehdi.sakout.fancybuttons.FancyButton
android:id="@+id/btn_add"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingBottom="5dp"
android:paddingLeft="20dp"
android:paddingRight="20dp"
android:paddingTop="5dp"
android:layout_marginLeft="10dp"
android:onClick="myclick"
fancy:fb_borderColor="#FFFFFF"
fancy:fb_borderWidth="1dp"
fancy:fb_defaultColor="#7ab800"
fancy:fb_focusColor="#9bd823"
fancy:fb_radius="30dp"
fancy:fb_text="Add Profile"
fancy:fb_textColor="#FFFFFF" />
<mehdi.sakout.fancybuttons.FancyButton
android:id="@+id/btn_change"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingBottom="5dp"
android:paddingLeft="10dp"
android:paddingRight="10dp"
android:paddingTop="5dp"
android:onClick="myclick"
android:layout_marginLeft="40dp"
fancy:fb_borderColor="#FFFFFF"
fancy:fb_borderWidth="1dp"
fancy:fb_defaultColor="#7ab800"
fancy:fb_focusColor="#9bd823"
fancy:fb_radius="30dp"
fancy:fb_text="Change Profile"
fancy:fb_textColor="#FFFFFF" />
</LinearLayout>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/listm0"
android:orientation="vertical"
android:onClick="myclick"
>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="40dp"
android:orientation="horizontal"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:weightSum="1">
<TextView
android:layout_weight="1"
android:id="@+id/txtPlace"
android:layout_height="match_parent"
android:layout_width="0dp"
android:text="@string/todays_prediction"
android:textSize="16dp"
android:paddingLeft="30dp"
android:gravity="center_vertical"/>
</LinearLayout>
</LinearLayout>
<View android:layout_height="1dp"
android:layout_width="match_parent"
android:background="@color/gray_light"></View>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/listm1"
android:orientation="vertical"
android:onClick="myclick"
>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="40dp"
android:orientation="horizontal"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:weightSum="1">
<TextView
android:layout_weight="1"
android:id="@+id/txtPlace"
android:layout_height="match_parent"
android:layout_width="0dp"
android:text="@string/astro_details"
android:textSize="16dp"
android:paddingLeft="30dp"
android:gravity="center_vertical"/>
</LinearLayout>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/listm5"
android:orientation="vertical"
android:onClick="myclick"
>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="40dp"
android:orientation="horizontal"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:weightSum="1">
<TextView
android:layout_weight="1"
android:id="@+id/txtPlace"
android:layout_height="match_parent"
android:layout_width="0dp"
android:text="@string/yogini"
android:textSize="16dp"
android:paddingLeft="30dp"
android:gravity="center_vertical"/>
</LinearLayout>
</LinearLayout>
<View android:layout_height="1dp"
android:layout_width="match_parent"
android:background="@color/gray_light"></View>
</LinearLayout>
</LinearLayout>
</ScrollView>
</LinearLayout>
</FrameLayout>
</android.support.v4.widget.DrawerLayout>
後logcat的垃圾在這裏。 –
只是發佈相關的代碼不是所有的項目 – Pooya
'txtUserName.findViewById(R.id.txtUserName);' - 這不是你如何使用'findViewById()'初始化'View'引用。它應該是'txtUserName =(TextView)findViewById(R.id.txtUserName);'。 –