Saturday, 31 March 2018

java - Android TextView SetText null object reference error




I am new to android and I am getting a



Attempt to invoke virtual method 'void android.widget.TextView.setText(java.lang.CharSequence)' on a null object reference


error on my app. I have read other questions on here like this one - java.lang.NullPointerException - setText on null object reference but I still get the same issue. My format is a little bit different as this is done in a Fragment and what I am doing is having a Custom Adapter from a listview. Here is my code



fragment_song_genre.xml





xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:bind="http://schemas.android.com/tools">



name="handler"
type="com.mgrmobi.joox.fragments.FilterSongGenreFragment" />




android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#000">

android:id="@+id/toprow"

android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="24dp">

android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:layout_marginLeft="16dp"

android:background="@null"
android:onClick="@{ handler.onCloseClicked }"
android:src="@drawable/search_close" />

android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:layout_marginLeft="80dp"

android:text="Genre"
android:textColor="#fff"
android:textSize="20sp" />

android:onClick="@{ handler.reset }"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"

android:layout_marginRight="15dp"
android:text="@string/reset"
android:textAllCaps="true"
android:textColor="#fff"
android:textSize="12sp" />



android:id="@+id/list"

android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@id/toprow"
android:layout_marginTop="24dp">







That is the main page and it has a Listview in the Bottom and here is the fragment that calls that xml layout



 public class FilterSongGenreFragment extends BaseFragment {

private SongGenreBinding binding;
private ArrayList genres;
String[] names= {"Johm","Paul","Mike"};

@Override
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) {

super.onCreateContextMenu(menu, v, menuInfo);
}


private static final String GENRES_KEY = "genres";

public static FilterSongGenreFragment newInstance(ArrayList genres) {

Bundle args = new Bundle();
args.putStringArrayList(GENRES_KEY, genres);

FilterSongGenreFragment fragment = new FilterSongGenreFragment();
fragment.setArguments(args);
return fragment;
}

@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
binding = DataBindingUtil.inflate(inflater, R.layout.fragment_song_genre, container, false);
binding.setHandler(this);

genres = getArguments().getStringArrayList(GENRES_KEY);

initList();
return binding.getRoot();
}

private void initList() {

// ArrayAdapter adapter = new ArrayAdapter(getActivity(), R.layout.genre_list_text, android.R.id.text1, genres);
FilterSongsAdapter filterSongsAdapter= new FilterSongsAdapter(getActivity(),names);



binding.list.setAdapter(filterSongsAdapter);

binding.list.setOnItemClickListener(new AdapterView.OnItemClickListener() {

@Override
public void onItemClick(AdapterView adapterView, View view,int position, long l) {
((SongFilterListener) getActivity()).onGenreSelected(genres.get(position));
/*

textView = (ImageView) view.findViewById(R.id.selected_genre);
textView.setVisibility(View.VISIBLE);
*/
// close();
}

});

}


private void close() {
getActivity().getFragmentManager().beginTransaction().remove(this).commit();
}

public void onCloseClicked(View view) {
close();
}

public void reset(View view) {
((SongFilterListener) getActivity()).onResetFilters();

close();
}

}


Here is the xml for the listview custom style genre_list_text.xml






xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">

android:id="@android:id/text1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"

android:gravity="center_vertical|left"
android:text="Genre"
android:paddingBottom="16dp"
android:paddingLeft="4dp"
android:paddingTop="16dp"
android:textColor="#fff"
android:textSize="16sp" />

android:id="@+id/selected_genre"

android:layout_height="24dp"
android:layout_width="24dp"
android:visibility="invisible"
android:src="@drawable/check_circle"
android:layout_marginStart="140dp"
android:layout_centerVertical="true" />





and here is my adapter and as stated before the error happens in the SetText area



  public class FilterSongsAdapter extends ArrayAdapter {

private Context c;
String[] names= {};
LayoutInflater inflater;

public FilterSongsAdapter(Context context,String[] names) {
super(context,R.layout.genre_list_text,names);

this.c=context;
this.names=names;

}

public static class ViewHolder {

TextView text1;



}



@Override
public View getView(int position,View convertView,ViewGroup parent) {
ViewHolder holder;
if(convertView==null){
holder = new ViewHolder();
inflater = (LayoutInflater) c.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

convertView = inflater.from(c).inflate(R.layout.genre_list_text, null);
// Initialize
holder.text1=(TextView) convertView.findViewById(R.id.text1);

convertView.setTag(holder);
}else {
holder = (ViewHolder) convertView.getTag();
}

holder.text1.setText(names[position]);


return convertView;
}




}



The SetText inside the GetView is throwing the exception every time, that TextView should have 3 objects names from the Names String Array which I have seen that it has. Based on the error I know that i'm setting the SetText before the FindByID method locates the TextView but I have moved things around and nothing works. Any help would be great . I also been following this tutorial for reference https://www.youtube.com/watch?v=99C_UpLcBRk . I do not know if maybe with a Fragment things are different.


Answer



in your layout you have given default id to TextView android:id="@android:id/text1".



and in JAVA file you are giving R.id.text1



make changes in xml or java.



ether change in xml from android:id="@android:id/text1" to android:id="@+id/text1"
or in java android.R.id.text1



java - How to make a JFrame button open another JFrame class in Netbeans?

I have a JFrame class and it was made in the design section on Netbeans. I am trying to make a log in button that takes closes the current frame and opens another, is there anyway I can do that?



I have tried:



JFrame frame = new JFrame(); 



But I want it to be editable in the design section!

java - compilation error with throwing exception




public class A {
public void f(A a) {
System.out.print("in A ");

}
}

public class B extends A {

public static void main(String[] args) {
B b = new B();
A a = new A();
b.f(b);
b.f(a);

}
}


Why by adding the following method in B class there will be a compilation error?



It's known that if method throws something, it doesn't have to be declared in the method header...



public void f(A a) { 
System.out.println("in B");

throw new java.io.IOExeption();
}

Answer




It's known that if method throws something, it doesn't have to be declared in the method header




That's only true for unchecked exceptions. Your method throws an IOException which is a checked exception so you must either catch it or declare that you throw it otherwise compilation will fail.


hyperlink - How do I link two HTML pages in a web application?

I've got two HTML pages, with one being an opening page for an application and the second being a login page. I have a button on the opening page and I want to be able to click on it and have it take me to the login page. How do I do this?

analysis - Why did the prisoners help Bruce? - Movies & TV



When Bruce is left broken and suffering in the Prison Pit, Bane explains that this is where he suffered and where Bruce will fully realize his failure with Gotham, and then be killed.



During his time there, the Doctor and his friend explain that Bane owns the prison, and their only duty is to keep him alive until Bane wants to kill him. However, they seem to turn around, heal his back and start encouraging him to find the will and ability to climb out of the pit.




The question here is, why?



Why would they disobey Bane and help Bruce?


Answer



As far as I see, it's still a prison and those prisoners who are under Bane's orders are trapped in the same way as Bruce Wayne was. There was no privilege which they enjoyed specifically more than him though.



Secondly Bruce was stuck there for a time that was time long enough to develop some camaraderie and seeing his passion and reaction to the way Gotham was destroyed in front of him could've appealed to their human side. It's often said prison is supposed to be creating a lot of retrospective thought processes for people who are imprisoned and I think they could've found a newer path after taking a look at Bruce Wayne and seeing him struggle against his wounds would've sparked a definite surge in them.



This third part I am not sure. Bane while he was in the pit as we see in the flashbacks fought with plenty of them protecting Talia. I think in a way he would've made enemies then. Since Bruce Wayne was against Bane. Enemy of my enemy theory could quite possibly have kicked in.



Get all html tags(parent and child tags) with id and class name with Javascript/JQuery



Does anybody knows how can I get all the HTML tags that exist in a page?



var items = document.getElementsByTagName("*");



This will get all the tags, but my requirement is




  • get a first parent tag and all the child tags under it


  • get the next parent tag and all the child tags under it and so on



I need to get the tags in a kind of tree-structure. Prefer to do that with Javascript or JQuery.



For example:





Example Page



Blabla















Should Return:



html





  1. head->title

  2. body-->h1,(div-->table-->tr-->td,td)


Answer



document.documentElement is the root of the tree (html). You can then get all of its child elements via children (childNodes would include non-Element children), and get their descendants in document order using querySelectorAll("*"):



var results = Array.prototype.map.call(
document.documentElement.children,
function(element) {
return element.querySelectorAll("*");

});


Live example



results will be an array with an entry for each direct child of the html element, where each element is a NodeList. If you want an array of arrays, you can use Array.from on the result of querySelectorAll (polyfilling it if necessary, as it's relatively new).



Of course there are a dozen ways to spin this. For instance, an array of objects instead:



var results = Array.prototype.map.call(

document.documentElement.children,
function(element) {
return {
element: element,
descendants: Array.from(element.querySelectorAll("*"))
};
});

android - How to create a alert message instead of toast

I have a problem. I have made an QR code scanner and i when he scan the code he put the result like toast but I want it as Alert Message. Can anyone help?



    import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Toast;


import com.google.zxing.Result;

import me.dm7.barcodescanner.zxing.ZXingScannerView;

public class MainActivity extends AppCompatActivity implements
ZXingScannerView.ResultHandler{
private ZXingScannerView zXingScannerView;
@Override
protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}

public void scan(View view){
zXingScannerView =new ZXingScannerView(getApplicationContext());
setContentView(zXingScannerView);
zXingScannerView.setResultHandler(this);
zXingScannerView.startCamera();


}

@Override
protected void onPause() {
super.onPause();
zXingScannerView.stopCamera();
}

@Override
public void handleResult(Result result) {

Toast.makeText(getApplicationContext(),result.getText(),Toast.LENGTH_SHORT).show();
zXingScannerView.resumeCameraPreview(this);

}
}


It is the last 4 lines in code.

casting - Why wasn't Tobey Maguire in The Amazing Spider-Man? - Movies & TV

In the Spider-Man franchise, Tobey Maguire is an outstanding performer as a Spider-Man and also reprised his role in the sequels Spider-Man...