来自其他活动的调用方法崩溃

时间:2017-02-26 08:15:40

标签: android searchview

我试图调用此方法

<label for="company-country">
  <span>Country</span>
</label>
<select id="company-country" name="company-country" required
    [(ngModel)]="currentCompany.location">   
  <option [value]="" >Select</option>  
  <option [value]="0" >Country</option>  
  <option [value]="1">Canada</option>
   ...
</select>

来自我的public void get() { tileView.smoothScaleFromCenter( 0.5000f ); } 我的基础活动,但它崩溃了。我该如何解决这个问题?

这是我的基本活动:

onItemClick

这是我的主要活动:

  public boolean onQueryTextChange(String newText) {
    showResults(newText + "*");
    return false;
}

public boolean onQueryTextSubmit(String query) {
    showResults(query + "*");
    return false;
}

public boolean onClose() {
    searchView.onActionViewCollapsed();
    showResults("");
    return false;
}
private void showResults(String query) {

    Cursor cursor = mDbHelper.searchPlaces((query != null ? query.toString() : "@@@@"));

    if (cursor == null) {
        //
    } else {
        // Specify the columns we want to display in the result
        String[] from = new String[]{
                PlacesDbAdapter.KEY_PLACES
        };

        // Specify the Corresponding layout elements where we want the columns to go
        int[] to = new int[]{R.id.scustomer
        };

        // Create a simple cursor adapter for the definitions and apply them to the ListView
        SimpleCursorAdapter places1 = new SimpleCursorAdapter(this, R.layout.customerresult, cursor, from, to);
        mListView.setAdapter(places1);

        // Define the on-click listener for the list items
        mListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                new MainActivity().get();
                searchView.setQuery("", true);
            }
        });
    }
}

这是我的logcat:

public void get() {
tileView.smoothScaleFromCenter( 0.5000f );
}}

2 个答案:

答案 0 :(得分:0)

创建活动需要时间。只要在new MainActivity()之后构建了您的活动,就无法创建或绑定与您的活动相关联的视图...因此NullPointerException

要使其正常工作,您应该将意图传递给MainActivity。然后,在活动生命周期的正确位置,您可以检查活动的起始意图中是否存在值,然后根据需要执行操作。

例如:

public static final String KEY_SCALE_FROM_CENTER = "scale_from_center";

@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
    searchView.setQuery("", true);
    Intent intent = new Intent(this, MainActivity.class);
    intent.putExtra(KEY_SCALE_FROM_CENTER, true);
    startActivity(intent);
}


public class MainActivity {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.your_view);
        if (getIntent().getBooleanExtra(KEY_SCALE_FROM_CENTER, false)) {
            get();
        }
    }
}

答案 1 :(得分:0)

最好以这种方式尝试

public class BaseActivity{
   //   ... fields....
   //Define ClickListner on base class  or just a setter
   OnItemClickListener onItemClickListener;
   //.... methods ...
   private void showResults(String query) {
    //   ... lines....

     \*mListView.setOnItemClickListener(new AdapterView.OnItemClickListener()          {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            new MainActivity().get();
            searchView.setQuery("", true);
         }
       });*/
      //replace above with this
      if(onItemClickListener!=null)
          mListView.setOnItemClickListener(onItemClickListener);
   }

   //generate setter for that
   public void setOnItemClickListener(OnItemClickListener onItemClickListener){
      this.onItemClickListener = onItemClickListener;
      mListView.setOnItemClickListener(onItemClickListener);
   }
}

public class MainActivity extends BaseActivity implements  OnItemClickListener{
  @Override
  protected void onCreate(Bundle savedInstanceState) {
      setContentview...
      setOnItemClickListener(this);
  }

  @Override
  public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
   get();
   searchView.setQuery("", true);
  }

}
主要活动中的

oncreate方法设置此项单击侦听器,以便您可以在多个类中使用基本活动

在行下面更新

//为列表项定义点击监听器 mListView.setOnItemClickListener(onItemClickListener); 既然您已经在主活动中定义了onClick,则不需要这样做: new MainActivity().get()

此处还要提到的一件事是新的MainActivity()创建mainActivity的对象并从中调用get方法 而不是当前被观看的活动 如果你仍然想要使用静态方法,我强烈建议不要这样做。