-1

这个应用程序在运行 4.1 的 Note 2 中运行良好,但在运行 htc 的 4.2 中我得到一个“线程退出未捕获的异常”和 NullPointerexception。我向你保证 id 匹配没有错误(我已经交叉检查了几次)。应用程序基本上是从谷歌输出最高分辨率的图像。这是日志猫

11-15 12:18:54.589: D/dalvikvm(14843): Late-enabling CheckJNI
11-15 12:18:54.700: W/ResourceType(14843): Skipping entry 0x7f040005 in package table 0 because it is not complex!
11-15 12:18:54.700: W/ResourceType(14843): Skipping entry 0x7f040005 in package table 0 because it is not complex!
11-15 12:18:54.710: W/ResourceType(14843): Skipping entry 0x7f040005 in package table 0 because it is not complex!
11-15 12:18:54.710: W/ResourceType(14843): Skipping entry 0x7f040005 in package table 0 because it is not complex!
11-15 12:18:54.750: D/TAG(14843): MainActivity has started.
11-15 12:18:54.800: D/libEGL(14843): loaded /system/lib/egl/libEGL_adreno200.so
11-15 12:18:54.800: D/libEGL(14843): loaded /system/lib/egl/libGLESv1_CM_adreno200.so
11-15 12:18:54.810: D/libEGL(14843): loaded /system/lib/egl/libGLESv2_adreno200.so
11-15 12:18:54.810: I/Adreno200-EGL(14843): <qeglDrvAPI_eglInitialize:269>: EGL 1.4 QUALCOMM build: AU_LINUX_ANDROID_JB_VANILLA.04.02.02.60.051_msm8960_JB_VANILLA_CL2997615_release_AU (CL2997615)
11-15 12:18:54.810: I/Adreno200-EGL(14843): Build Date: 04/11/13 Thu
11-15 12:18:54.810: I/Adreno200-EGL(14843): Local Branch: 
11-15 12:18:54.810: I/Adreno200-EGL(14843): Remote Branch: quic/mako_jb_mr1
11-15 12:18:54.810: I/Adreno200-EGL(14843): Local Patches: NONE
11-15 12:18:54.810: I/Adreno200-EGL(14843): Reconstruct Branch: AU_LINUX_ANDROID_JB_VANILLA.04.02.02.60.051 +  NOTHING
11-15 12:18:54.850: D/OpenGLRenderer(14843): Enabling debug mode 0
11-15 12:18:58.704: D/TAG(14843): In button listener
11-15 12:18:58.704: D/TAG(14843): String has been added!!!
11-15 12:18:58.714: D/TAG(14843): Making the fragment now!!
11-15 12:18:58.714: D/TAG(14843): Arguments passed!!
11-15 12:18:58.714: D/TAG(14843): Fragment made!!
11-15 12:18:58.724: D/AndroidRuntime(14843): Shutting down VM
11-15 12:18:58.734: W/dalvikvm(14843): threadid=1: thread exiting with uncaught exception (group=0x41bba930)
11-15 12:18:58.754: E/AndroidRuntime(14843): FATAL EXCEPTION: main
11-15 12:18:58.754: E/AndroidRuntime(14843): java.lang.NullPointerException
11-15 12:18:58.754: E/AndroidRuntime(14843):    at android.widget.ArrayAdapter.createViewFromResource(ArrayAdapter.java:394)
11-15 12:18:58.754: E/AndroidRuntime(14843):    at android.widget.ArrayAdapter.getView(ArrayAdapter.java:362)
11-15 12:18:58.754: E/AndroidRuntime(14843):    at android.widget.AbsListView.obtainView(AbsListView.java:2159)
11-15 12:18:58.754: E/AndroidRuntime(14843):    at android.widget.ListView.makeAndAddView(ListView.java:1831)
11-15 12:18:58.754: E/AndroidRuntime(14843):    at android.widget.ListView.fillDown(ListView.java:674)
11-15 12:18:58.754: E/AndroidRuntime(14843):    at android.widget.ListView.fillFromTop(ListView.java:735)
11-15 12:18:58.754: E/AndroidRuntime(14843):    at android.widget.ListView.layoutChildren(ListView.java:1652)
11-15 12:18:58.754: E/AndroidRuntime(14843):    at android.widget.AbsListView.onLayout(AbsListView.java:1994)
11-15 12:18:58.754: E/AndroidRuntime(14843):    at android.view.View.layout(View.java:14015)
11-15 12:18:58.754: E/AndroidRuntime(14843):    at android.view.ViewGroup.layout(ViewGroup.java:4373)
11-15 12:18:58.754: E/AndroidRuntime(14843):    at android.support.v4.widget.DrawerLayout.onLayout(DrawerLayout.java:702)
11-15 12:18:58.754: E/AndroidRuntime(14843):    at android.view.View.layout(View.java:14015)
11-15 12:18:58.754: E/AndroidRuntime(14843):    at android.view.ViewGroup.layout(ViewGroup.java:4373)
11-15 12:18:58.754: E/AndroidRuntime(14843):    at android.widget.FrameLayout.onLayout(FrameLayout.java:448)
11-15 12:18:58.754: E/AndroidRuntime(14843):    at android.view.View.layout(View.java:14015)
11-15 12:18:58.754: E/AndroidRuntime(14843):    at android.view.ViewGroup.layout(ViewGroup.java:4373)
11-15 12:18:58.754: E/AndroidRuntime(14843):    at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1663)
11-15 12:18:58.754: E/AndroidRuntime(14843):    at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1521)
11-15 12:18:58.754: E/AndroidRuntime(14843):    at android.widget.LinearLayout.onLayout(LinearLayout.java:1434)
11-15 12:18:58.754: E/AndroidRuntime(14843):    at android.view.View.layout(View.java:14015)
11-15 12:18:58.754: E/AndroidRuntime(14843):    at android.view.ViewGroup.layout(ViewGroup.java:4373)
11-15 12:18:58.754: E/AndroidRuntime(14843):    at android.widget.FrameLayout.onLayout(FrameLayout.java:448)
11-15 12:18:58.754: E/AndroidRuntime(14843):    at android.view.View.layout(View.java:14015)
11-15 12:18:58.754: E/AndroidRuntime(14843):    at android.view.ViewGroup.layout(ViewGroup.java:4373)
11-15 12:18:58.754: E/AndroidRuntime(14843):    at android.view.ViewRootImpl.performLayout(ViewRootImpl.java:1892)
11-15 12:18:58.754: E/AndroidRuntime(14843):    at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1711)
11-15 12:18:58.754: E/AndroidRuntime(14843):    at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:989)
11-15 12:18:58.754: E/AndroidRuntime(14843):    at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:4351)
11-15 12:18:58.754: E/AndroidRuntime(14843):    at android.view.Choreographer$CallbackRecord.run(Choreographer.java:749)
11-15 12:18:58.754: E/AndroidRuntime(14843):    at android.view.Choreographer.doCallbacks(Choreographer.java:562)
11-15 12:18:58.754: E/AndroidRuntime(14843):    at android.view.Choreographer.doFrame(Choreographer.java:532)
11-15 12:18:58.754: E/AndroidRuntime(14843):    at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:735)
11-15 12:18:58.754: E/AndroidRuntime(14843):    at android.os.Handler.handleCallback(Handler.java:725)
11-15 12:18:58.754: E/AndroidRuntime(14843):    at android.os.Handler.dispatchMessage(Handler.java:92)
11-15 12:18:58.754: E/AndroidRuntime(14843):    at android.os.Looper.loop(Looper.java:137)
11-15 12:18:58.754: E/AndroidRuntime(14843):    at android.app.ActivityThread.main(ActivityThread.java:5227)
11-15 12:18:58.754: E/AndroidRuntime(14843):    at java.lang.reflect.Method.invokeNative(Native Method)
11-15 12:18:58.754: E/AndroidRuntime(14843):    at java.lang.reflect.Method.invoke(Method.java:511)
11-15 12:18:58.754: E/AndroidRuntime(14843):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:795)
11-15 12:18:58.754: E/AndroidRuntime(14843):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:562)
11-15 12:18:58.754: E/AndroidRuntime(14843):    at dalvik.system.NativeStart.main(Native Method)

MainActivity.java

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Log.d("TAG","MainActivity has started.");
    drawer_enter=(EditText)findViewById(R.id.drawer_enter);
    list=(ListView)findViewById(R.id.left_drawer);
    //image_button=(Button)findViewById(R.id.image_button);
    //image_button.setOnClickListener(button);
    drawer_enter.setOnEditorActionListener(new TextView.OnEditorActionListener() {
        @Override
        public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
            if (actionId == EditorInfo.IME_ACTION_DONE) {

                InputMethodManager imm = (InputMethodManager)getSystemService(
                      Context.INPUT_METHOD_SERVICE);
                imm.hideSoftInputFromWindow(drawer_enter.getWindowToken(), 0);

        Log.d("TAG","In button listener");
        inflateString(drawer_enter.getText().toString());
        MakeFrag();

                return true;
            }
            return false;
        }
    });


    drawer=(DrawerLayout)findViewById(R.id.drawer_layout);
    listen=new ActionBarDrawerToggle(this,drawer,R.drawable.ic_launcher,0,0){
        public void onDrawerOpened(View drawerView) {
             getActionBar().setTitle("History");
         }
        public void onDrawerClosed(View drawerView) {
             getActionBar().setTitle("Best Google Image");
         }
    };
    drawer.setDrawerShadow(R.drawable.drawer_shadow, GravityCompat.START);
    drawer.setDrawerListener(listen);
    list.setOnItemClickListener(new DrawerItemClickListener());

}

/*private OnClickListener button=new OnClickListener() {
    @Override
    public void onClick(View theView) {
        InputMethodManager imm = (InputMethodManager)getSystemService(
                  Context.INPUT_METHOD_SERVICE);
            imm.hideSoftInputFromWindow(drawer_enter.getWindowToken(), 0);

    Log.d("TAG","In button listener");

    //int num=inflateString(drawer_enter.getText().toString());
    MakeFrag();
    }
};*/

private class DrawerItemClickListener implements ListView.OnItemClickListener{

    @Override
    public void onItemClick(AdapterView<?> arg0, View arg1, int position,
            long arg3) {
        MakeDrawerFrag(position);
        //drawer.closeDrawer(arg0);
    }

}

public void MakeFrag(){
    Log.d("TAG", "Making the fragment now!!");
    Fragment fragment=new ImageFragment();
    Bundle args=new Bundle();
    args.putString(ImageFragment.ARG_IMAGE_SEARCH,drawer_enter.getText().toString());
    fragment.setArguments(args);

    FragmentManager fragmentManager=getFragmentManager();
    Log.d("TAG", "Arguments passed!!");
    fragmentManager.beginTransaction().replace(R.id.frame_layout, fragment).addToBackStack(null).commit();
    Log.d("TAG", "Fragment made!!");

}



private void inflateString(String name)
{
    int i=0;
    for(i=0;i<names.length;i++){
        if(names[i]==null){
            names[i]=name;
            break;
        }
    }
    list.setAdapter(new ArrayAdapter<String>(this,R.layout.list_item,names));
    Log.d("TAG","String has been added!!!");
}

ImageFragment.java

public class ImageFragment extends Fragment{
        public static final String ARG_IMAGE_SEARCH = "image_search";
        public Bitmap done=null;
        private String search="",newSearch="";
        private ImageView photo=null;
        /*public ImageFragment() {
            // Empty constructor required for fragment subclasses
        }*/

        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                Bundle savedInstanceState) {
            Log.d("TAG","making the view of the Fragment!!!");
            search=getArguments().getString(ARG_IMAGE_SEARCH);
            int i;

            for(i=0;i<search.length();i++){
                if(search.charAt(i)==' '){
                    newSearch+="%20";
                }
                else{
                    newSearch+=search.charAt(i);

                }
            }
            Log.d("TAG",newSearch);
            new ImageLoader().execute(newSearch);
            View rootView = inflater.inflate(R.layout.image_view, container, false);
            photo=(ImageView) rootView.findViewById(R.id.image_view);
            photo.setImageBitmap(done);
            getActivity().setTitle(getArguments().getString(ARG_IMAGE_SEARCH));
            return rootView;
        }





private class ImageLoader extends AsyncTask<String,Void,Void>{
@Override
protected Void doInBackground(String... args) {
    // TODO Auto-generated method stub
    try{
        Log.d("TAG", "Background thread has started!!");
        URL url=new URL("https://ajax.googleapis.com/ajax/services/search/images?" +
                "v=1.0&q="+args[0]);
        HttpURLConnection connection=(HttpURLConnection) url.openConnection();
        String urlData;
        StringBuilder builder=new StringBuilder();
        BufferedReader reader=new BufferedReader(new InputStreamReader(connection.getInputStream()));
        while((urlData=reader.readLine()) != null){
            builder.append(urlData);
        }
        JSONObject first=new JSONObject(builder.toString());
        int i,larg = 0;
        for(i=0;i<2;i++){
            if((first.getJSONObject("responseData").getJSONArray("results").getJSONObject(i).getInt("width"))>first.getJSONObject("responseData").getJSONArray("results").getJSONObject(i+1).getInt("width")){
                larg=i;
            }
            else larg=i+1;
        }
        String image=first.getJSONObject("responseData").getJSONArray("results").getJSONObject(larg).getString("url");
        URL imgUrl = new URL(image);
        HttpURLConnection imgConnection=(HttpURLConnection)imgUrl.openConnection();
        InputStream input=imgConnection.getInputStream();
        done=BitmapFactory.decodeStream(input);
        Log.d("TAG","Bitmap has been formed!!");

    } catch (ParseException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IllegalStateException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (JSONException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } 
    return null;


    }
protected void onPostExecute(Void result){
    photo.setImageBitmap(done);
}
4

1 回答 1

0

我相信您的问题出在此处的 ImageFragment onCreate 中:

search=getArguments().getString(ARG_IMAGE_SEARCH);

我相信你得到一个空指针是因为你没有得到任何参数——你没有得到任何参数是因为 Fragment 没有接收它们——我将在最后向你展示如何修复它。

只是为了确保它没有得到任何参数,这样做 - 注释掉片段的 onCreateView 中的所有内容,然后使它像这样:

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.image_view, container, false);

try {
    search=getArguments().getString(ARG_IMAGE_SEARCH);
    Log.d("onCreateView",search); // this will print out the search string to logcat
    } catch (Exception e) {
    e.printStackTrace();  // or throw an exception and print it to logcat
    }

return rootView;
}

这将验证搜索是否正在获取参数(它可能不是)并打印我们的搜索字符串,或者抛出异常。如果它抛出异常,则继续...

要解决此问题,您首先需要将其添加到您的 Fragment:

static ImageFragment newInstance(String string){
    ImageFragment fragment = new ImageFragment();

    Bundle args = new Bundle();
            args.putString(ImageFragment.ARG_IMAGE_SEARCH, string);

    fragment.setArguments(args);
    return fragment;
}

然后在你的activity中,启动一个新的ImageFragment,你这样称呼它:

Fragment newImageFrag = ImageFragment.newInstance(WHATEVER_STRING_ARGUMENT_YOU_NEED_TO_PASS);
// from here add or replace the fragment as normal

完成这两件事后,当 newImageFrag 通过 newInstance 和传递的字符串创建时,您将能够调用

search=getArguments().getString(ARG_IMAGE_SEARCH);

并真正得到论据。我希望我正确地理解了你的问题,我希望这会有所帮助。

于 2013-11-15T08:09:26.933 回答