Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/210.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
试图理解这行java代码_Java_Android - Fatal编程技术网

试图理解这行java代码

试图理解这行java代码,java,android,Java,Android,这是android应用程序教程的一些java代码。我不明白这到底在做什么。我知道这是在给mCallback赋值但是什么?为什么OnHeadlineSelectedListener在括号中,而activity对象就在它后面,这到底是怎么回事 mCallback = (OnHeadlineSelectedListener) activity; /* *版权所有(C)2012安卓开源项目 * *根据Apache许可证2.0版(以下简称“许可证”)获得许可; *除非遵守许可证,否则不得使用此文件。 *

这是android应用程序教程的一些java代码。我不明白这到底在做什么。我知道这是在给mCallback赋值但是什么?为什么OnHeadlineSelectedListener在括号中,而activity对象就在它后面,这到底是怎么回事

mCallback = (OnHeadlineSelectedListener) activity;
/*
*版权所有(C)2012安卓开源项目
*
*根据Apache许可证2.0版(以下简称“许可证”)获得许可;
*除非遵守许可证,否则不得使用此文件。
*您可以通过以下方式获得许可证副本:
*
*      http://www.apache.org/licenses/LICENSE-2.0
*
*除非适用法律要求或书面同意,软件
*根据许可证进行的分发是按“原样”进行分发的,
*无任何明示或暗示的保证或条件。
*请参阅许可证以了解管理权限和权限的特定语言
*许可证下的限制。
*/
包com.vizoplex.my.first.app;
导入android.app.Activity;
导入android.os.Build;
导入android.os.Bundle;
导入android.support.v4.app.ListFragment;
导入android.view.view;
导入android.widget.ArrayAdapter;
导入android.widget.ListView;
公共类HeadlinesFragment扩展ListFragment{
OnHeadlineSelectedListener McCallback;
//容器活动必须实现此接口,以便frag可以传递消息
HeadlineSelectedListener上的公共接口{
/**选择列表项时由HeadlinesFragment调用*/
所选部件上的公共无效(int位置);
}
@凌驾
创建时的公共void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
//我们需要为比蜂巢更旧的设备使用不同的列表项布局
int layout=Build.VERSION.SDK\u int>=Build.VERSION\u code.h?
android.R.layout.simple_list_item_activated_1:android.R.layout.simple_list_item_1;
//使用Ipsum标题数组为列表视图创建数组适配器
setListAdapter(新的ArrayAdapter(getActivity(),layout,Ipsum.Headlines));
}
@凌驾
public void onStart(){
super.onStart();
//在双窗格布局中,将listview设置为高亮显示选定的列表项
//(我们在onStart期间执行此操作,因为此时listview可用。)
if(getFragmentManager().findFragmentById(R.id.article_fragment)!=null){
getListView().setChoiceMode(ListView.CHOICE\u MODE\u SINGLE);
}
}
@凌驾
公共事务主任(活动){
超级转速计(活动);
//这确保容器活动已实现
//回调接口。如果不是,则抛出异常。
试一试{
mCallback=(OnHeadlineSelectedListener)活动;
}catch(ClassCastException e){
抛出新的ClassCastException(activity.toString()
+“必须实现OnHeadlineSelectedListener”);
}
}
@凌驾
public void onListItemClick(列表视图l、视图v、整数位置、长id){
//通知所选项目的父活动
mCallback.OnArticle已选定(位置);
//将项目设置为选中状态,以便在双窗格布局中高亮显示
getListView().setItemChecked(位置,true);
}
}

活动
正在被分配给
OnHeadlineSelectedListener
对象。

它正在将活动强制转换为OnHeadlineSelectedListener。假设活动在HeadlineSelectedListener上实现。这样做是为了让您可以直接调用HeadlineSelectedListener接口中的方法。

它试图将该活动强制转换为OnHeadlineSelectedListener,可能是因为该实际活动扩展了
Listener
。通过这种方式,mCallback充当正确定义回调的侦听器

编辑

你有:

/*
 * Copyright (C) 2012 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.vizoplex.my.first.app;

import android.app.Activity;
import android.os.Build;
import android.os.Bundle;
import android.support.v4.app.ListFragment;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;

public class HeadlinesFragment extends ListFragment {
    OnHeadlineSelectedListener mCallback;

    // The container Activity must implement this interface so the frag can deliver messages
    public interface OnHeadlineSelectedListener {
        /** Called by HeadlinesFragment when a list item is selected */
        public void onArticleSelected(int position);
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        // We need to use a different list item layout for devices older than Honeycomb
        int layout = Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB ?
                android.R.layout.simple_list_item_activated_1 : android.R.layout.simple_list_item_1;

        // Create an array adapter for the list view, using the Ipsum headlines array
        setListAdapter(new ArrayAdapter<String>(getActivity(), layout, Ipsum.Headlines));
    }

    @Override
    public void onStart() {
        super.onStart();

        // When in two-pane layout, set the listview to highlight the selected list item
        // (We do this during onStart because at the point the listview is available.)
        if (getFragmentManager().findFragmentById(R.id.article_fragment) != null) {
            getListView().setChoiceMode(ListView.CHOICE_MODE_SINGLE);
        }
    }

    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);

        // This makes sure that the container activity has implemented
        // the callback interface. If not, it throws an exception.
        try {
            mCallback = (OnHeadlineSelectedListener) activity;
        } catch (ClassCastException e) {
            throw new ClassCastException(activity.toString()
                    + " must implement OnHeadlineSelectedListener");
        }
    }

    @Override
    public void onListItemClick(ListView l, View v, int position, long id) {
        // Notify the parent activity of selected item
        mCallback.onArticleSelected(position);

        // Set the item as checked to be highlighted when in two-pane layout
        getListView().setItemChecked(position, true);
    }
}
它正在尝试将
活动
强制转换为
OnHeadlineSelectedListener
。如果
activity
未实现此侦听器,则会引发异常。信息非常简单:

try {
    mCallback = (OnHeadlineSelectedListener) activity;
} catch (ClassCastException e) {
    throw new ClassCastException(activity.toString()
            + " must implement OnHeadlineSelectedListener");
}

最近我已经从Java中转换出来了,但在我看来,这行代码似乎正在将
activity
转换为类型
OnHeadlineSelectedListener
。强制转换将一种类型转换为另一种类型,如果类型不兼容,则抛出一个异常(一种或另一种形式)

语法如下:

activity.toString()+ " must implement OnHeadlineSelectedListener"

我认为mCallback对象是activity对象类的子类。 因此,此语句将activity对象强制转换为更特定的mCallback对象

有关类型转换的转换的详细信息,请参见。 例如: 你有两种方法的动物界面,run和eat

variable = (Type) variableOfType2;
还有类,它实现了它,比如说,Cat类。Cat类有一些额外的方法,scratch和mew

public interface Animal {
    public String run();
    public String eat(String food);
}
还有,你有狗类,它有吠叫法

public class Cat implements Animal{
   public String run(){
     return "Run as a cat";
   }
   public String eat(){
     return "Eat fish";
   }
   public String mew(){
     return "MEW!!!!";
   }
   public String scratch(){
     return "scratch-scratch-scratch";
   }
}
所以,很多时候你需要和你的动物一起工作,就像和一些精确的动物(狗或猫)一样。但是,没有你的帮助,我不能做这个角色。所以,你必须告诉它你到底想要什么:

public class Dog implements Animal{
   public String run(){
     return "Run as a dog";
   }
   public String eat(){
     return "Eat meat";
   }
   public String bark(){
     return "BARK!!!!";
   }
}

学习Java非常有用。如果你真的对理解这些东西感兴趣,你应该读一读。关于铸造工作原理的完整解释也可以在本书中找到。

您可以发布教程或一些周围的线条吗?我可以告诉你,它正在将变量“activity”引用的对象强制转换为HeadlineSelectedListener上的类,然后将该值设置为变量mCallback,但是如果没有上下文,这对我来说并不意味着什么。谢谢,这就是我需要知道的。我是java和android开发的新手。我记得强制转换变量,但我不想把它也应用到对象上。如果你愿意,对象只是复杂(或简单)的变量。您可以将
int
转换为
float
public void Show{
  Cat cat = new Cat(); //you have a cat
  Dog dog = new Dog(); //and a dog
  Animal animal = null; //and animal
  dog = (Dog)animal; //and here is where compiler can't convert animal to dog without your help.
  Animal animal2 = cat; //But this casting it can make without your help, as it exactly knows what to do.
}