ImageView可能会返回Null?

时间:2016-08-03 08:05:29

标签: android nullable findviewbyid

我有一个片段是半个图像,然后是一半文本。

尝试分配ImageViewTextView,但它说:

  

可能会给出

当我运行应用程序时,它不会运行。

我包含了片段的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 个答案:

答案 0 :(得分:1)

对于片段,你应该首先在onCreateView方法中膨胀你的xml,你得到你的视图,你可以找到你相应的ImageView:

View myView = inflater.inflate(R.layout.frag, container, false);

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

return myView;

在xml中你应该为iamgeView添加一个id来获取它:

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

答案 1 :(得分:0)

  

尝试分配ImageView和textView,但它说可能会给   null错误,当我运行应用程序时,它不会运行。

这是一个lint警告,通知您findViewById可能返回null。如果您的xml包含您要查找的id,则无需担心。

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

getView()会返回View onCreateView次返回,因此您无法在getView()内使用onCreateView。改为覆盖onViewCreate及其参数View以执行findViewById来电

答案 2 :(得分:0)

在片段中,您首先需要对视图进行渗透,然后将值分配给其内容

示例:

@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;
}