2016-08-03 6 views
-5

私は、半分のイメージと半分のテキストである断片を持っています。 ImageViewがNullを返す可能性がありますか?

ImageViewTextViewを割り当てるしようとしているが、それは言う:

がnull

を与えるかもしれないと私はアプリを実行すると、それは実行されません。

私はフラグメントのためのJavaとXMLファイルを含んでおり、mainActivity javaを含んでいました。

フラグメントコード:

import android.media.Image; 
import android.os.Bundle; 
import android.provider.ContactsContract; 
import android.support.v4.app.Fragment; 
import android.view.LayoutInflater; 
import android.view.ViewGroup; 
import android.view.View; 
import android.widget.TextView; 
import android.widget.ImageView; 
import android.widget.TextView; 
import org.w3c.dom.Text; 
import android.view.View; 


    public class headercode extends Fragment implements Runnable{ 

     ImageView image; 
     TextView text; 


     @Override 
     public View onCreateView(LayoutInflater inflater, ViewGroup container, 
           Bundle savedInstanceState) { 

      if(getView().findViewById(R.id.imageView) != null) 
      { 

       image = (ImageView)getView().findViewById(R.id.imageView); 

      } 
      return inflater.inflate(R.layout.frag, container, false); 
     } 

     public void run(){ 

      text.setText("Test"); 

     } 
    } 

レイアウトXML:

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="vertical" android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:background="#692f2f"> 
    <ImageView 
     android:layout_width="match_parent" 
     android:layout_height="300dp" /> 
    <TextView 
     android:layout_width="match_parent" 
     android:layout_height="269dp" 
     android:id="@+id/text"/> 
</LinearLayout> 

MainActivityコード:フラグメントのために

import android.os.Bundle; 
import android.support.design.widget.FloatingActionButton; 
import android.support.design.widget.Snackbar; 
import android.view.View; 
import android.support.design.widget.NavigationView; 
import android.support.v4.view.GravityCompat; 
import android.support.v4.widget.DrawerLayout; 
import android.support.v7.app.ActionBarDrawerToggle; 
import android.support.v7.app.AppCompatActivity; 
import android.support.v7.widget.Toolbar; 
import android.view.Menu; 
import android.view.MenuItem; 
import android.widget.TextView; 
import android.support.v4.app.FragmentTransaction; 
import android.support.v4.app.FragmentManager; 
import android.widget.FrameLayout; 
import android.widget.Button; 



public class MainActivity extends AppCompatActivity 
     implements NavigationView.OnNavigationItemSelectedListener { 




    Button button; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 
     Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); 



     DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout); 
     ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
       this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close); 
     drawer.setDrawerListener(toggle); 
     toggle.syncState(); 



     NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view); 
     navigationView.setNavigationItemSelectedListener(this); 

     if (findViewById(R.id.fragment) != null){ 

      headercode header = new headercode(); 
      getSupportFragmentManager().beginTransaction().add(R.id.fragment,header).commit(); 




     } 

    } 

    @Override 
    public void onBackPressed() { 
     DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout); 
     if (drawer.isDrawerOpen(GravityCompat.START)) { 
      drawer.closeDrawer(GravityCompat.START); 
     } else { 
      super.onBackPressed(); 
     } 
    } 

    @Override 
    public boolean onCreateOptionsMenu(Menu menu) { 
     // Inflate the menu; this adds items to the action bar if it is present. 
     getMenuInflater().inflate(R.menu.main, menu);//This is the setting top right 
     return true; 
    } 


    @SuppressWarnings("StatementWithEmptyBody") 
    @Override 
    public boolean onNavigationItemSelected(MenuItem item) { 
     // Handle navigation view item clicks here. 
     int id = item.getItemId(); 

     if (id == R.id.Technology) { 



     } else if (id == R.id.Opinion) { 




     } else if (id == R.id.travel) { 



     } else if (id == R.id.politics) { 



     }else if(id == R.id.Home){ 



     } 

     DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout); 
     drawer.closeDrawer(GravityCompat.START); 
     return true; 
    } 

} 
+3

を重複した質問Android Studioとは関係ありません(私はあなたの投稿を喜んで編集します:P)。質問を投稿する前に必ずGoogleにお問い合わせください。時間が節約できます。 http://stackoverflow.com/questions/10697915/why-custom-image-view-s-findviewbyid-returns-null – Sufian

+0

リントの警告は 'findViewById()'が 'null'を返すことができると言うことです。これは、nullが必要であることを知らせることです。ここでそれを読んでください - http://stackoverflow.com/questions/31936907/why-android-studio-has-this-nullable-thing。しかし、あなたの問題はこれを読んでいる - http://stackoverflow.com/questions/10697915/why-custom-image-view-s-findviewbyid-returns-null – Sufian

答えて

1

あなたがあなたを取得し、onCreateView方法で最初にあなたのxmlを膨らませる必要がありますあなたは対応するImageViewを見つけることができます:

あなたはそれを得るためにiamgeViewにIDを追加する必要があり、XMLで
View myView = inflater.inflate(R.layout.frag, container, false); 

ImageView myImage = (ImageView) myView.findViewById(R.layout.my_image); 

return myView; 

:ImageViewのとのTextViewを割り当てるしようと

<ImageView 
     android:id="@+id/my_image" 
     android:layout_width="match_parent" 
     android:layout_height="300dp" /> 
+3

彼はまた、彼のイメージビューに 'id'を与えていない上記。 – Shaishav

0

が、それは ヌルエラーを与え、ときかもしれませんと言います私はそれが実行されないアプリを実行します。

findViewByIdがnullを返す可能性があることを通知するリント警告です。あなたのxmlにあなたが探しているidが含まれていれば、心配することはありません。

@Override 
public View onCreateView(LayoutInflater inflater, ViewGroup container, 
          Bundle savedInstanceState) { 
     if(getView().findViewById(R.id.imageView) != null) 

getView()onCreateView戻りViewを返し、それゆえあなたはonCreateViewgetView()を使用することはできません。代わりにonViewCreateをオーバーライドし、ビューをinfalteするフラグメントます最初の必要性では、あなたのfindViewById通話

0

を実行するには、そのパラメータViewと、その内容に

例の値をASSING:あなたが求めている

@Override 
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 

View rootView = inflater.inflate(R.layout.frag, container, false); 
ImageView image = (ImageView) rootView.findViewById(R.id.imageView); 
TextView tv = (TextView) rootView.findViewById(R.id.yourTextViewId); 
tv.setText("Test"); 

// Here you will perform all the actions that you want to perform and then in the end return the view like below 

return rootView; 
} 
関連する問題